home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / bash / bash-110 / mintve~1 / readline.zoo / readline / readline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-03  |  147.2 KB  |  6,362 lines

  1. /* readline.c -- a general facility for reading lines of input
  2.    with emacs style editing and completion. */
  3.  
  4. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  5.  
  6.    This file contains the Readline Library (the Library), a set of
  7.    routines for providing Emacs style line input to programs that ask
  8.    for it.
  9.  
  10.    The Library is free software; you can redistribute it and/or modify
  11.    it under the terms of the GNU General Public License as published by
  12.    the Free Software Foundation; either version 1, or (at your option)
  13.    any later version.
  14.  
  15.    The Library is distributed in the hope that it will be useful, but
  16.    WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.    General Public License for more details.
  19.  
  20.    The GNU General Public License is often shipped with GNU software, and
  21.    is generally kept in a file called COPYING or LICENSE.  If you do not
  22.    have a copy of the license, write to the Free Software Foundation,
  23.    675 Mass Ave, Cambridge, MA 02139, USA. */
  24.  
  25. /* Remove these declarations when we have a complete libgnu.a. */
  26. /* #define STATIC_MALLOC */
  27. #if !defined (STATIC_MALLOC)
  28. extern char *xmalloc (), *xrealloc ();
  29. #else
  30. static char *xmalloc (), *xrealloc ();
  31. #endif /* STATIC_MALLOC */
  32.  
  33. #include <stdio.h>
  34. #include <sys/types.h>
  35. #include <fcntl.h>
  36. #include <sys/file.h>
  37. #include <signal.h>
  38.  
  39. #if defined (__GNUC__)
  40. #  define alloca __builtin_alloca
  41. #else
  42. #  if defined (sparc) || defined (HAVE_ALLOCA_H)
  43. #    include <alloca.h>
  44. #  endif
  45. #endif
  46.  
  47. #if defined (HAVE_UNISTD_H)
  48. #  include <unistd.h>
  49. #endif
  50.  
  51. #define NEW_TTY_DRIVER
  52. #define HAVE_BSD_SIGNALS
  53. /* #define USE_XON_XOFF */
  54.  
  55. /* Some USG machines have BSD signal handling (sigblock, sigsetmask, etc.) */
  56. #if defined (USG) && !defined (hpux)
  57. #undef HAVE_BSD_SIGNALS
  58. #endif
  59.  
  60. /* System V machines use termio. */
  61. #if !defined (_POSIX_VERSION)
  62. #  if defined (USG) || defined (hpux) || defined (Xenix) || defined (sgi) || defined (DGUX)
  63. #    undef NEW_TTY_DRIVER
  64. #    define TERMIO_TTY_DRIVER
  65. #    include <termio.h>
  66. #    if !defined (TCOON)
  67. #      define TCOON 1
  68. #    endif
  69. #  endif /* USG || hpux || Xenix || sgi || DUGX */
  70. #endif /* !_POSIX_VERSION */
  71.  
  72. /* Posix systems use termios and the Posix signal functions. */
  73. #if defined (_POSIX_VERSION)
  74. #  undef NEW_TTY_DRIVER
  75. #  define TERMIOS_TTY_DRIVER
  76. #  define HAVE_POSIX_SIGNALS
  77. #  include <termios.h>
  78. #  if !defined (O_NDELAY)
  79. #    define O_NDELAY O_NONBLOCK    /* Posix-style non-blocking i/o */
  80. #  endif /* O_NDELAY */
  81. #endif /* _POSIX_VERSION */
  82.  
  83. /* Other (BSD) machines use sgtty. */
  84. #if defined (NEW_TTY_DRIVER)
  85. #include <sgtty.h>
  86. #endif
  87.  
  88. #include <errno.h>
  89. extern int errno;
  90.  
  91. #include <setjmp.h>
  92. #include <sys/stat.h>
  93.  
  94. /* Posix macro to check file in statbuf for directory-ness. */
  95. #if defined (S_IFDIR) && !defined (S_ISDIR)
  96. #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
  97. #endif
  98.  
  99. /* These next are for filename completion.  Perhaps this belongs
  100.    in a different place. */
  101. #include <pwd.h>
  102. #if defined (USG) && !defined (isc386) && !defined (sgi)
  103. struct passwd *getpwuid (), *getpwent ();
  104. #endif
  105.  
  106. /* #define HACK_TERMCAP_MOTION */
  107.  
  108. #if !defined (USG)
  109. #  include <sys/dir.h>
  110. #else  /* USG */
  111. #  if defined (Xenix)
  112. #    include <sys/ndir.h>
  113. #  else
  114. #    if defined (hpux)
  115. #      include <ndir.h>
  116. #    else
  117. #      include <dirent.h>
  118. #      define direct dirent
  119. #      define d_namlen d_reclen
  120. #    endif  /* hpux */
  121. #  endif  /* Xenix */
  122. #endif  /* USG */
  123.  
  124. #if defined (USG) && defined (TIOCGWINSZ)
  125. #  include <sys/stream.h>
  126. #  if defined (USGr4) || defined (USGr3)
  127. #    include <sys/ptem.h>
  128. #  endif /* USGr4 */
  129. #endif /* USG && TIOCGWINSZ */
  130.  
  131. /* Some standard library routines. */
  132. #include "readline.h"
  133. #include "history.h"
  134.  
  135. #ifndef digit
  136. #define digit(c)  ((c) >= '0' && (c) <= '9')
  137. #endif
  138.  
  139. #ifndef isletter
  140. #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
  141. #endif
  142.  
  143. #ifndef digit_value
  144. #define digit_value(c) ((c) - '0')
  145. #endif
  146.  
  147. #ifndef member
  148. #define member(c, s) ((c) ? index ((s), (c)) : 0)
  149. #endif
  150.  
  151. #ifndef isident
  152. #define isident(c) ((isletter(c) || digit(c) || c == '_'))
  153. #endif
  154.  
  155. #ifndef exchange
  156. #define exchange(x, y) {int temp = x; x = y; y = temp;}
  157. #endif
  158.  
  159. #if !defined (rindex)
  160. extern char *rindex ();
  161. #endif /* rindex */
  162.  
  163. #if !defined (index)
  164. extern char *index ();
  165. #endif /* index */
  166.  
  167. extern char *getenv ();
  168. extern char *tilde_expand ();
  169.  
  170. static update_line ();
  171. static void output_character_function ();
  172. static delete_chars ();
  173. static insert_some_chars ();
  174.  
  175. #if defined (VOID_SIGHANDLER)
  176. #  define sighandler void
  177. #else
  178. #  define sighandler int
  179. #endif /* VOID_SIGHANDLER */
  180.  
  181. /* This typedef is equivalant to the one for Function; it allows us
  182.    to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
  183. typedef sighandler SigHandler ();
  184.  
  185. /* If on, then readline handles signals in a way that doesn't screw. */
  186. #define HANDLE_SIGNALS
  187.  
  188.  
  189. /* **************************************************************** */
  190. /*                                    */
  191. /*            Line editing input utility            */
  192. /*                                    */
  193. /* **************************************************************** */
  194.  
  195. /* A pointer to the keymap that is currently in use.
  196.    By default, it is the standard emacs keymap. */
  197. Keymap keymap = emacs_standard_keymap;
  198.  
  199. #define no_mode -1
  200. #define vi_mode 0
  201. #define emacs_mode 1
  202.  
  203. /* The current style of editing. */
  204. int rl_editing_mode = emacs_mode;
  205.  
  206. /* Non-zero if the previous command was a kill command. */
  207. static int last_command_was_kill = 0;
  208.  
  209. /* The current value of the numeric argument specified by the user. */
  210. int rl_numeric_arg = 1;
  211.  
  212. /* Non-zero if an argument was typed. */
  213. int rl_explicit_arg = 0;
  214.  
  215. /* Temporary value used while generating the argument. */
  216. int rl_arg_sign = 1;
  217.  
  218. /* Non-zero means we have been called at least once before. */
  219. static int rl_initialized = 0;
  220.  
  221. /* If non-zero, this program is running in an EMACS buffer. */
  222. static char *running_in_emacs = (char *)NULL;
  223.  
  224. /* The current offset in the current input line. */
  225. int rl_point;
  226.  
  227. /* Mark in the current input line. */
  228. int rl_mark;
  229.  
  230. /* Length of the current input line. */
  231. int rl_end;
  232.  
  233. /* Make this non-zero to return the current input_line. */
  234. int rl_done;
  235.  
  236. /* The last function executed by readline. */
  237. Function *rl_last_func = (Function *)NULL;
  238.  
  239. /* Top level environment for readline_internal (). */
  240. static jmp_buf readline_top_level;
  241.  
  242. /* The streams we interact with. */
  243. static FILE *in_stream, *out_stream;
  244.  
  245. /* The names of the streams that we do input and output to. */
  246. FILE *rl_instream = stdin, *rl_outstream = stdout;
  247.  
  248. /* Non-zero means echo characters as they are read. */
  249. int readline_echoing_p = 1;
  250.  
  251. /* Current prompt. */
  252. char *rl_prompt;
  253.  
  254. /* The number of characters read in order to type this complete command. */
  255. int rl_key_sequence_length = 0;
  256.  
  257. /* If non-zero, then this is the address of a function to call just
  258.    before readline_internal () prints the first prompt. */
  259. Function *rl_startup_hook = (Function *)NULL;
  260.  
  261. /* If non-zero, then this is the address of a function to call when
  262.    completing on a directory name.  The function is called with
  263.    the address of a string (the current directory name) as an arg. */
  264. Function *rl_symbolic_link_hook = (Function *)NULL;
  265.  
  266. /* What we use internally.  You should always refer to RL_LINE_BUFFER. */
  267. static char *the_line;
  268.  
  269. /* The character that can generate an EOF.  Really read from
  270.    the terminal driver... just defaulted here. */
  271. static int eof_char = CTRL ('D');
  272.  
  273. /* Non-zero makes this the next keystroke to read. */
  274. int rl_pending_input = 0;
  275.  
  276. /* Pointer to a useful terminal name. */
  277. char *rl_terminal_name = (char *)NULL;
  278.  
  279. /* Line buffer and maintenence. */
  280. char *rl_line_buffer = (char *)NULL;
  281. int rl_line_buffer_len = 0;
  282. #define DEFAULT_BUFFER_SIZE 256
  283.  
  284.  
  285. /* **************************************************************** */
  286. /*                                    */
  287. /*            `Forward' declarations              */
  288. /*                                    */
  289. /* **************************************************************** */
  290.  
  291. /* Non-zero means do not parse any lines other than comments and
  292.    parser directives. */
  293. static unsigned char parsing_conditionalized_out = 0;
  294.  
  295. /* Caseless strcmp (). */
  296. static int stricmp (), strnicmp ();
  297.  
  298. /* Non-zero means to save keys that we dispatch on in a kbd macro. */
  299. static int defining_kbd_macro = 0;
  300.  
  301.  
  302. /* **************************************************************** */
  303. /*                                    */
  304. /*            Top Level Functions                */
  305. /*                                    */
  306. /* **************************************************************** */
  307.  
  308. static void rl_prep_terminal (), rl_deprep_terminal ();
  309.  
  310. /* Read a line of input.  Prompt with PROMPT.  A NULL PROMPT means
  311.    none.  A return value of NULL means that EOF was encountered. */
  312. char *
  313. readline (prompt)
  314.      char *prompt;
  315. {
  316.   char *readline_internal ();
  317.   char *value;
  318.  
  319.   rl_prompt = prompt;
  320.  
  321.   /* If we are at EOF return a NULL string. */
  322.   if (rl_pending_input == EOF)
  323.     {
  324.       rl_pending_input = 0;
  325.       return ((char *)NULL);
  326.     }
  327.  
  328.   rl_initialize ();
  329.   rl_prep_terminal ();
  330.  
  331. #if defined (HANDLE_SIGNALS)
  332.   rl_set_signals ();
  333. #endif
  334.  
  335.   value = readline_internal ();
  336.   rl_deprep_terminal ();
  337.  
  338. #if defined (HANDLE_SIGNALS)
  339.   rl_clear_signals ();
  340. #endif
  341.  
  342.   return (value);
  343. }
  344.  
  345. /* Read a line of input from the global rl_instream, doing output on
  346.    the global rl_outstream.
  347.    If rl_prompt is non-null, then that is our prompt. */
  348. char *
  349. readline_internal ()
  350. {
  351.   int lastc, c, eof_found;
  352.  
  353.   in_stream = rl_instream; out_stream = rl_outstream;
  354.   lastc = eof_found = 0;
  355.  
  356.   if (rl_startup_hook)
  357.     (*rl_startup_hook) ();
  358.  
  359.   if (!readline_echoing_p)
  360.     {
  361.       if (rl_prompt)
  362.     {
  363.       fprintf (out_stream, "%s", rl_prompt);
  364.       fflush (out_stream);
  365.     }
  366.     }
  367.   else
  368.     {
  369.       rl_on_new_line ();
  370.       rl_redisplay ();
  371. #if defined (VI_MODE)
  372.       if (rl_editing_mode == vi_mode)
  373.     rl_vi_insertion_mode ();
  374. #endif /* VI_MODE */
  375.     }
  376.  
  377.   while (!rl_done)
  378.     {
  379.       int lk = last_command_was_kill;
  380.       int code = setjmp (readline_top_level);
  381.  
  382.       if (code)
  383.     rl_redisplay ();
  384.  
  385.       if (!rl_pending_input)
  386.     {
  387.       /* Then initialize the argument and number of keys read. */
  388.       rl_init_argument ();
  389.       rl_key_sequence_length = 0;
  390.     }
  391.  
  392.       c = rl_read_key ();
  393.  
  394.       /* EOF typed to a non-blank line is a <NL>. */
  395.       if (c == EOF && rl_end)
  396.     c = NEWLINE;
  397.  
  398.       /* The character eof_char typed to blank line, and not as the
  399.      previous character is interpreted as EOF. */
  400.       if (((c == eof_char && lastc != c) || c == EOF) && !rl_end)
  401.     {
  402.       eof_found = 1;
  403.       break;
  404.     }
  405.  
  406.       lastc = c;
  407.       rl_dispatch (c, keymap);
  408.  
  409.       /* If there was no change in last_command_was_kill, then no kill
  410.      has taken place.  Note that if input is pending we are reading
  411.      a prefix command, so nothing has changed yet. */
  412.       if (!rl_pending_input)
  413.     {
  414.       if (lk == last_command_was_kill)
  415.         last_command_was_kill = 0;
  416.     }
  417.  
  418. #if defined (VI_MODE)
  419.       /* In vi mode, when you exit insert mode, the cursor moves back
  420.      over the previous character.  We explicitly check for that here. */
  421.       if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap)
  422.     rl_vi_check ();
  423. #endif /* VI_MODE */
  424.  
  425.       if (!rl_done)
  426.     rl_redisplay ();
  427.     }
  428.  
  429.   /* Restore the original of this history line, iff the line that we
  430.      are editing was originally in the history, AND the line has changed. */
  431.   {
  432.     HIST_ENTRY *entry = current_history ();
  433.  
  434.     if (entry && rl_undo_list)
  435.       {
  436.     char *temp = savestring (the_line);
  437.     rl_revert_line ();
  438.     entry = replace_history_entry (where_history (), the_line,
  439.                        (HIST_ENTRY *)NULL);
  440.     free_history_entry (entry);
  441.  
  442.     strcpy (the_line, temp);
  443.     free (temp);
  444.       }
  445.   }
  446.  
  447.   /* At any rate, it is highly likely that this line has an undo list.  Get
  448.      rid of it now. */
  449.   if (rl_undo_list)
  450.     free_undo_list ();
  451.  
  452.   if (eof_found)
  453.     return (char *)NULL;
  454.   else
  455.     return (savestring (the_line));
  456. }
  457.  
  458.  
  459. /* **************************************************************** */
  460. /*                                        */
  461. /*               Signal Handling                          */
  462. /*                                    */
  463. /* **************************************************************** */
  464.  
  465. #if defined (SIGWINCH)
  466. static SigHandler *old_sigwinch = (SigHandler *)NULL;
  467.  
  468. static sighandler
  469. rl_handle_sigwinch (sig)
  470.      int sig;
  471. {
  472.   char *term;
  473.  
  474.   term = rl_terminal_name;
  475.  
  476.   if (readline_echoing_p)
  477.     {
  478.       if (!term)
  479.     term = getenv ("TERM");
  480.       if (!term)
  481.     term = "dumb";
  482.       rl_reset_terminal (term);
  483. #if defined (NOTDEF)
  484.       crlf ();
  485.       rl_forced_update_display ();
  486. #endif /* NOTDEF */
  487.     }
  488.  
  489.   if (old_sigwinch &&
  490.       old_sigwinch != (SigHandler *)SIG_IGN &&
  491.       old_sigwinch != (SigHandler *)SIG_DFL)
  492.     (*old_sigwinch) (sig);
  493. #if !defined (VOID_SIGHANDLER)
  494.   return (0);
  495. #endif /* VOID_SIGHANDLER */
  496. }
  497. #endif  /* SIGWINCH */
  498.  
  499. #if defined (HANDLE_SIGNALS)
  500. /* Interrupt handling. */
  501. static SigHandler
  502.   *old_int  = (SigHandler *)NULL,
  503.   *old_tstp = (SigHandler *)NULL,
  504.   *old_ttou = (SigHandler *)NULL,
  505.   *old_ttin = (SigHandler *)NULL,
  506.   *old_cont = (SigHandler *)NULL,
  507.   *old_alrm = (SigHandler *)NULL;
  508.  
  509. /* Handle an interrupt character. */
  510. static sighandler
  511. rl_signal_handler (sig)
  512.      int sig;
  513. {
  514. #if !defined (HAVE_BSD_SIGNALS)
  515.   /* Since the signal will not be blocked while we are in the signal
  516.      handler, ignore it until rl_clear_signals resets the catcher. */
  517.   if (sig == SIGINT)
  518.     signal (sig, SIG_IGN);
  519. #endif /* !HAVE_BSD_SIGNALS */
  520.  
  521.   switch (sig)
  522.     {
  523.     case SIGINT:
  524.       free_undo_list ();
  525.       rl_clear_message ();
  526.       rl_init_argument ();
  527.  
  528. #if defined (SIGTSTP)
  529.     case SIGTSTP:
  530.     case SIGTTOU:
  531.     case SIGTTIN:
  532. #endif /* SIGTSTP */
  533.     case SIGALRM:
  534.       rl_clean_up_for_exit ();
  535.       rl_deprep_terminal ();
  536.       rl_clear_signals ();
  537.       rl_pending_input = 0;
  538.  
  539.       kill (getpid (), sig);
  540.  
  541. #if defined (HAVE_POSIX_SIGNALS)
  542.       {
  543.     sigset_t set;
  544.  
  545.     sigemptyset (&set);
  546.     sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
  547.       }
  548. #else
  549. #if defined (HAVE_BSD_SIGNALS)
  550.       sigsetmask (0);
  551. #endif /* HAVE_BSD_SIGNALS */
  552. #endif /* HAVE_POSIX_SIGNALS */
  553.  
  554.       rl_prep_terminal ();
  555.       rl_set_signals ();
  556.     }
  557.  
  558. #if !defined (VOID_SIGHANDLER)
  559.   return (0);
  560. #endif /* !VOID_SIGHANDLER */
  561. }
  562.  
  563. rl_set_signals ()
  564. {
  565.   old_int = (SigHandler *)signal (SIGINT, rl_signal_handler);
  566.   if (old_int == (SigHandler *)SIG_IGN)
  567.     signal (SIGINT, SIG_IGN);
  568.  
  569.   old_alrm = (SigHandler *)signal (SIGALRM, rl_signal_handler);
  570.   if (old_alrm == (SigHandler *)SIG_IGN)
  571.     signal (SIGALRM, SIG_IGN);
  572.  
  573. #if defined (SIGTSTP)
  574.   old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler);
  575.   if (old_tstp == (SigHandler *)SIG_IGN)
  576.     signal (SIGTSTP, SIG_IGN);
  577. #endif
  578. #if defined (SIGTTOU)
  579.   old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler);
  580.   old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler);
  581.  
  582.   if (old_tstp == (SigHandler *)SIG_IGN)
  583.     {
  584.       signal (SIGTTOU, SIG_IGN);
  585.       signal (SIGTTIN, SIG_IGN);
  586.     }
  587. #endif
  588.  
  589. #if defined (SIGWINCH)
  590.   old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch);
  591. #endif
  592. }
  593.  
  594. rl_clear_signals ()
  595. {
  596.   signal (SIGINT, old_int);
  597.   signal (SIGALRM, old_alrm);
  598.  
  599. #if defined (SIGTSTP)
  600.   signal (SIGTSTP, old_tstp);
  601. #endif
  602.  
  603. #if defined (SIGTTOU)
  604.   signal (SIGTTOU, old_ttou);
  605.   signal (SIGTTIN, old_ttin);
  606. #endif
  607.  
  608. #if defined (SIGWINCH)
  609.       signal (SIGWINCH, old_sigwinch);
  610. #endif
  611. }
  612. #endif  /* HANDLE_SIGNALS */
  613.  
  614.  
  615. /* **************************************************************** */
  616. /*                                    */
  617. /*            Character Input Buffering               */
  618. /*                                    */
  619. /* **************************************************************** */
  620.  
  621. #if defined (USE_XON_XOFF)
  622. /* If the terminal was in xoff state when we got to it, then xon_char
  623.    contains the character that is supposed to start it again. */
  624. static int xon_char, xoff_state;
  625. #endif /* USE_XON_XOFF */
  626.  
  627. static int pop_index = 0, push_index = 0, ibuffer_len = 511;
  628. static unsigned char ibuffer[512];
  629.  
  630. /* Non-null means it is a pointer to a function to run while waiting for
  631.    character input. */
  632. Function *rl_event_hook = (Function *)NULL;
  633.  
  634. #define any_typein (push_index != pop_index)
  635.  
  636. /* Add KEY to the buffer of characters to be read. */
  637. rl_stuff_char (key)
  638.      int key;
  639. {
  640.   if (key == EOF)
  641.     {
  642.       key = NEWLINE;
  643.       rl_pending_input = EOF;
  644.     }
  645.   ibuffer[push_index++] = key;
  646.   if (push_index >= ibuffer_len)
  647.     push_index = 0;
  648. }
  649.  
  650. /* Return the amount of space available in the
  651.    buffer for stuffing characters. */
  652. int
  653. ibuffer_space ()
  654. {
  655.   if (pop_index > push_index)
  656.     return (pop_index - push_index);
  657.   else
  658.     return (ibuffer_len - (push_index - pop_index));
  659. }
  660.  
  661. /* Get a key from the buffer of characters to be read.
  662.    Return the key in KEY.
  663.    Result is KEY if there was a key, or 0 if there wasn't. */
  664. int
  665. rl_get_char (key)
  666.      int *key;
  667. {
  668.   if (push_index == pop_index)
  669.     return (0);
  670.  
  671.   *key = ibuffer[pop_index++];
  672.  
  673.   if (pop_index >= ibuffer_len)
  674.     pop_index = 0;
  675.  
  676.   return (1);
  677. }
  678.  
  679. /* Stuff KEY into the *front* of the input buffer.
  680.    Returns non-zero if successful, zero if there is
  681.    no space left in the buffer. */
  682. int
  683. rl_unget_char (key)
  684.      int key;
  685. {
  686.   if (ibuffer_space ())
  687.     {
  688.       pop_index--;
  689.       if (pop_index < 0)
  690.     pop_index = ibuffer_len - 1;
  691.       ibuffer[pop_index] = key;
  692.       return (1);
  693.     }
  694.   return (0);
  695. }
  696.  
  697. /* If a character is available to be read, then read it
  698.    and stuff it into IBUFFER.  Otherwise, just return. */
  699. rl_gather_tyi ()
  700. {
  701.   int tty = fileno (in_stream);
  702.   register int tem, result = -1;
  703.   long chars_avail;
  704.   char input;
  705.  
  706. #if defined (FIONREAD)
  707.   result = ioctl (tty, FIONREAD, &chars_avail);
  708. #endif
  709.  
  710.   if (result == -1)
  711.     {
  712.       int flags;
  713.  
  714.       flags = fcntl (tty, F_GETFL, 0);
  715.  
  716.       fcntl (tty, F_SETFL, (flags | O_NDELAY));
  717.       chars_avail = read (tty, &input, 1);
  718.  
  719.       fcntl (tty, F_SETFL, flags);
  720.       if (chars_avail == -1 && errno == EAGAIN)
  721.     return;
  722.     }
  723.  
  724.   /* If there's nothing available, don't waste time trying to read
  725.      something. */
  726.   if (chars_avail == 0)
  727.     return;
  728.  
  729.   tem = ibuffer_space ();
  730.  
  731.   if (chars_avail > tem)
  732.     chars_avail = tem;
  733.  
  734.   /* One cannot read all of the available input.  I can only read a single
  735.      character at a time, or else programs which require input can be
  736.      thwarted.  If the buffer is larger than one character, I lose.
  737.      Damn! */
  738.   if (tem < ibuffer_len)
  739.     chars_avail = 0;
  740.  
  741.   if (result != -1)
  742.     {
  743.       while (chars_avail--)
  744.     rl_stuff_char (rl_getc (in_stream));
  745.     }
  746.   else
  747.     {
  748.       if (chars_avail)
  749.     rl_stuff_char (input);
  750.     }
  751. }
  752.  
  753. static int next_macro_key ();
  754. /* Read a key, including pending input. */
  755. int
  756. rl_read_key ()
  757. {
  758.   int c;
  759.  
  760.   rl_key_sequence_length++;
  761.  
  762.   if (rl_pending_input)
  763.     {
  764.       c = rl_pending_input;
  765.       rl_pending_input = 0;
  766.     }
  767.   else
  768.     {
  769.       /* If input is coming from a macro, then use that. */
  770.       if (c = next_macro_key ())
  771.     return (c);
  772.  
  773.       /* If the user has an event function, then call it periodically. */
  774.       if (rl_event_hook)
  775.     {
  776.       while (rl_event_hook && !rl_get_char (&c))
  777.         {
  778.           (*rl_event_hook) ();
  779.           rl_gather_tyi ();
  780.         }
  781.     }
  782.       else
  783.     {
  784.       if (!rl_get_char (&c))
  785.         c = rl_getc (in_stream);
  786.     }
  787.     }
  788.  
  789.   return (c);
  790. }
  791.  
  792. /* I'm beginning to hate the declaration rules for various compilers. */
  793. static void add_macro_char (), with_macro_input ();
  794.  
  795. /* Do the command associated with KEY in MAP.
  796.    If the associated command is really a keymap, then read
  797.    another key, and dispatch into that map. */
  798. rl_dispatch (key, map)
  799.      register int key;
  800.      Keymap map;
  801. {
  802.  
  803.   if (defining_kbd_macro)
  804.     add_macro_char (key);
  805.  
  806.   if (key > 127 && key < 256)
  807.     {
  808.       if (map[ESC].type == ISKMAP)
  809.     {
  810.       map = (Keymap)map[ESC].function;
  811.       key -= 128;
  812.       rl_dispatch (key, map);
  813.     }
  814.       else
  815.     ding ();
  816.       return;
  817.     }
  818.  
  819.   switch (map[key].type)
  820.     {
  821.     case ISFUNC:
  822.       {
  823.     Function *func = map[key].function;
  824.  
  825.     if (func != (Function *)NULL)
  826.       {
  827.         /* Special case rl_do_lowercase_version (). */
  828.         if (func == rl_do_lowercase_version)
  829.           {
  830.         rl_dispatch (to_lower (key), map);
  831.         return;
  832.           }
  833.  
  834.         (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
  835.  
  836.         /* If we have input pending, then the last command was a prefix
  837.            command.  Don't change the state of rl_last_func.  Otherwise,
  838.            remember the last command executed in this variable. */
  839.         if (!rl_pending_input)
  840.           rl_last_func = map[key].function;
  841.       }
  842.     else
  843.       {
  844.         rl_abort ();
  845.         return;
  846.       }
  847.       }
  848.       break;
  849.  
  850.     case ISKMAP:
  851.       if (map[key].function != (Function *)NULL)
  852.     {
  853.       int newkey;
  854.  
  855.       rl_key_sequence_length++;
  856.       newkey = rl_read_key ();
  857.       rl_dispatch (newkey, (Keymap)map[key].function);
  858.     }
  859.       else
  860.     {
  861.       rl_abort ();
  862.       return;
  863.     }
  864.       break;
  865.  
  866.     case ISMACR:
  867.       if (map[key].function != (Function *)NULL)
  868.     {
  869.       char *macro;
  870.  
  871.       macro = savestring ((char *)map[key].function);
  872.       with_macro_input (macro);
  873.       return;
  874.     }
  875.       break;
  876.     }
  877. }
  878.  
  879.  
  880. /* **************************************************************** */
  881. /*                                    */
  882. /*            Hacking Keyboard Macros             */
  883. /*                                    */
  884. /* **************************************************************** */
  885.  
  886. /* The currently executing macro string.  If this is non-zero,
  887.    then it is a malloc ()'ed string where input is coming from. */
  888. static char *executing_macro = (char *)NULL;
  889.  
  890. /* The offset in the above string to the next character to be read. */
  891. static int executing_macro_index = 0;
  892.  
  893. /* The current macro string being built.  Characters get stuffed
  894.    in here by add_macro_char (). */
  895. static char *current_macro = (char *)NULL;
  896.  
  897. /* The size of the buffer allocated to current_macro. */
  898. static int current_macro_size = 0;
  899.  
  900. /* The index at which characters are being added to current_macro. */
  901. static int current_macro_index = 0;
  902.  
  903. /* A structure used to save nested macro strings.
  904.    It is a linked list of string/index for each saved macro. */
  905. struct saved_macro {
  906.   struct saved_macro *next;
  907.   char *string;
  908.   int index;
  909. };
  910.  
  911. /* The list of saved macros. */
  912. struct saved_macro *macro_list = (struct saved_macro *)NULL;
  913.  
  914. /* Forward declarations of static functions.  Thank you C. */
  915. static void push_executing_macro (), pop_executing_macro ();
  916.  
  917. /* This one has to be declared earlier in the file. */
  918. /* static void add_macro_char (); */
  919.  
  920. /* Set up to read subsequent input from STRING.
  921.    STRING is free ()'ed when we are done with it. */
  922. static void
  923. with_macro_input (string)
  924.      char *string;
  925. {
  926.   push_executing_macro ();
  927.   executing_macro = string;
  928.   executing_macro_index = 0;
  929. }
  930.  
  931. /* Return the next character available from a macro, or 0 if
  932.    there are no macro characters. */
  933. static int
  934. next_macro_key ()
  935. {
  936.   if (!executing_macro)
  937.     return (0);
  938.  
  939.   if (!executing_macro[executing_macro_index])
  940.     {
  941.       pop_executing_macro ();
  942.       return (next_macro_key ());
  943.     }
  944.  
  945.   return (executing_macro[executing_macro_index++]);
  946. }
  947.  
  948. /* Save the currently executing macro on a stack of saved macros. */
  949. static void
  950. push_executing_macro ()
  951. {
  952.   struct saved_macro *saver;
  953.  
  954.   saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
  955.   saver->next = macro_list;
  956.   saver->index = executing_macro_index;
  957.   saver->string = executing_macro;
  958.  
  959.   macro_list = saver;
  960. }
  961.  
  962. /* Discard the current macro, replacing it with the one
  963.    on the top of the stack of saved macros. */
  964. static void
  965. pop_executing_macro ()
  966. {
  967.   if (executing_macro)
  968.     free (executing_macro);
  969.  
  970.   executing_macro = (char *)NULL;
  971.   executing_macro_index = 0;
  972.  
  973.   if (macro_list)
  974.     {
  975.       struct saved_macro *disposer = macro_list;
  976.       executing_macro = macro_list->string;
  977.       executing_macro_index = macro_list->index;
  978.       macro_list = macro_list->next;
  979.       free (disposer);
  980.     }
  981. }
  982.  
  983. /* Add a character to the macro being built. */
  984. static void
  985. add_macro_char (c)
  986.      int c;
  987. {
  988.   if (current_macro_index + 1 >= current_macro_size)
  989.     {
  990.       if (!current_macro)
  991.     current_macro = (char *)xmalloc (current_macro_size = 25);
  992.       else
  993.     current_macro =
  994.       (char *)xrealloc (current_macro, current_macro_size += 25);
  995.     }
  996.  
  997.   current_macro[current_macro_index++] = c;
  998.   current_macro[current_macro_index] = '\0';
  999. }
  1000.  
  1001. /* Begin defining a keyboard macro.
  1002.    Keystrokes are recorded as they are executed.
  1003.    End the definition with rl_end_kbd_macro ().
  1004.    If a numeric argument was explicitly typed, then append this
  1005.    definition to the end of the existing macro, and start by
  1006.    re-executing the existing macro. */
  1007. rl_start_kbd_macro (ignore1, ignore2)
  1008.      int ignore1, ignore2;
  1009. {
  1010.   if (defining_kbd_macro)
  1011.     rl_abort ();
  1012.  
  1013.   if (rl_explicit_arg)
  1014.     {
  1015.       if (current_macro)
  1016.     with_macro_input (savestring (current_macro));
  1017.     }
  1018.   else
  1019.     current_macro_index = 0;
  1020.  
  1021.   defining_kbd_macro = 1;
  1022. }
  1023.  
  1024. /* Stop defining a keyboard macro.
  1025.    A numeric argument says to execute the macro right now,
  1026.    that many times, counting the definition as the first time. */
  1027. rl_end_kbd_macro (count, ignore)
  1028.      int count, ignore;
  1029. {
  1030.   if (!defining_kbd_macro)
  1031.     rl_abort ();
  1032.  
  1033.   current_macro_index -= (rl_key_sequence_length - 1);
  1034.   current_macro[current_macro_index] = '\0';
  1035.  
  1036.   defining_kbd_macro = 0;
  1037.  
  1038.   rl_call_last_kbd_macro (--count, 0);
  1039. }
  1040.  
  1041. /* Execute the most recently defined keyboard macro.
  1042.    COUNT says how many times to execute it. */
  1043. rl_call_last_kbd_macro (count, ignore)
  1044.      int count, ignore;
  1045. {
  1046.   if (!current_macro)
  1047.     rl_abort ();
  1048.  
  1049.   while (count--)
  1050.     with_macro_input (savestring (current_macro));
  1051. }
  1052.  
  1053.  
  1054. /* **************************************************************** */
  1055. /*                                    */
  1056. /*            Initializations                 */
  1057. /*                                    */
  1058. /* **************************************************************** */
  1059.  
  1060. /* Initliaze readline (and terminal if not already). */
  1061. rl_initialize ()
  1062. {
  1063.   extern char *rl_display_prompt;
  1064.  
  1065.   /* If we have never been called before, initialize the
  1066.      terminal and data structures. */
  1067.   if (!rl_initialized)
  1068.     {
  1069.       readline_initialize_everything ();
  1070.       rl_initialized++;
  1071.     }
  1072.  
  1073.   /* Initalize the current line information. */
  1074.   rl_point = rl_end = 0;
  1075.   the_line = rl_line_buffer;
  1076.   the_line[0] = 0;
  1077.  
  1078.   /* We aren't done yet.  We haven't even gotten started yet! */
  1079.   rl_done = 0;
  1080.  
  1081.   /* Tell the history routines what is going on. */
  1082.   start_using_history ();
  1083.  
  1084.   /* Make the display buffer match the state of the line. */
  1085.   {
  1086.     extern char *rl_display_prompt;
  1087.     extern int forced_display;
  1088.  
  1089.     rl_on_new_line ();
  1090.  
  1091.     rl_display_prompt = rl_prompt ? rl_prompt : "";
  1092.     forced_display = 1;
  1093.   }
  1094.  
  1095.   /* No such function typed yet. */
  1096.   rl_last_func = (Function *)NULL;
  1097.  
  1098.   /* Parsing of key-bindings begins in an enabled state. */
  1099.   parsing_conditionalized_out = 0;
  1100. }
  1101.  
  1102. /* Initialize the entire state of the world. */
  1103. readline_initialize_everything ()
  1104. {
  1105.   /* Find out if we are running in Emacs. */
  1106.   running_in_emacs = getenv ("EMACS");
  1107.  
  1108.   /* Allocate data structures. */
  1109.   if (!rl_line_buffer)
  1110.     rl_line_buffer =
  1111.       (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
  1112.  
  1113.   /* Initialize the terminal interface. */
  1114.   init_terminal_io ((char *)NULL);
  1115.  
  1116.   /* Bind tty characters to readline functions. */
  1117.   readline_default_bindings ();
  1118.  
  1119.   /* Initialize the function names. */
  1120.   rl_initialize_funmap ();
  1121.  
  1122.   /* Read in the init file. */
  1123.   rl_read_init_file ((char *)NULL);
  1124.  
  1125.   /* If the completion parser's default word break characters haven't
  1126.      been set yet, then do so now. */
  1127.   {
  1128.     extern char *rl_completer_word_break_characters;
  1129.     extern char *rl_basic_word_break_characters;
  1130.  
  1131.     if (rl_completer_word_break_characters == (char *)NULL)
  1132.       rl_completer_word_break_characters = rl_basic_word_break_characters;
  1133.   }
  1134. }
  1135.  
  1136. /* If this system allows us to look at the values of the regular
  1137.    input editing characters, then bind them to their readline
  1138.    equivalents. */
  1139. readline_default_bindings ()
  1140. {
  1141.  
  1142. #if defined (NEW_TTY_DRIVER)
  1143.   struct sgttyb ttybuff;
  1144.   int tty = fileno (rl_instream);
  1145.  
  1146.   if (ioctl (tty, TIOCGETP, &ttybuff) != -1)
  1147.     {
  1148.       int erase = ttybuff.sg_erase, kill = ttybuff.sg_kill;
  1149.  
  1150.       if (erase != -1 && keymap[erase].type == ISFUNC)
  1151.     keymap[erase].function = rl_rubout;
  1152.  
  1153.       if (kill != -1 && keymap[kill].type == ISFUNC)
  1154.     keymap[kill].function = rl_unix_line_discard;
  1155.     }
  1156.  
  1157. #if defined (TIOCGLTC)
  1158.   {
  1159.     struct ltchars lt;
  1160.  
  1161.     if (ioctl (tty, TIOCGLTC, <) != -1)
  1162.       {
  1163.     int erase = lt.t_werasc, nextc = lt.t_lnextc;
  1164.  
  1165.     if (erase != -1 && keymap[erase].type == ISFUNC)
  1166.       keymap[erase].function = rl_unix_word_rubout;
  1167.  
  1168.     if (nextc != -1 && keymap[nextc].type == ISFUNC)
  1169.       keymap[nextc].function = rl_quoted_insert;
  1170.       }
  1171.   }
  1172. #endif /* TIOCGLTC */
  1173. #else /* not NEW_TTY_DRIVER */
  1174.  
  1175. #if defined (TERMIOS_TTY_DRIVER)
  1176.   struct termios ttybuff;
  1177. #else
  1178.   struct termio ttybuff;
  1179. #endif /* TERMIOS_TTY_DRIVER */
  1180.   int tty = fileno (rl_instream);
  1181.  
  1182. #if defined (TERMIOS_TTY_DRIVER)
  1183.   if (tcgetattr (tty, &ttybuff) != -1)
  1184. #else
  1185.   if (ioctl (tty, TCGETA, &ttybuff) != -1)
  1186. #endif /* !TERMIOS_TTY_DRIVER */
  1187.     {
  1188.       int erase = ttybuff.c_cc[VERASE];
  1189.       int kill = ttybuff.c_cc[VKILL];
  1190.  
  1191.       if (erase != -1 && keymap[(unsigned char)erase].type == ISFUNC)
  1192.     keymap[(unsigned char)erase].function = rl_rubout;
  1193.  
  1194.       if (kill != -1 && keymap[(unsigned char)kill].type == ISFUNC)
  1195.     keymap[(unsigned char)kill].function = rl_unix_line_discard;
  1196.     }
  1197. #endif /* NEW_TTY_DRIVER */
  1198. }
  1199.  
  1200.  
  1201. /* **************************************************************** */
  1202. /*                                    */
  1203. /*            Numeric Arguments                */
  1204. /*                                    */
  1205. /* **************************************************************** */
  1206.  
  1207. /* Handle C-u style numeric args, as well as M--, and M-digits. */
  1208.  
  1209. /* Add the current digit to the argument in progress. */
  1210. rl_digit_argument (ignore, key)
  1211.      int ignore, key;
  1212. {
  1213.   rl_pending_input = key;
  1214.   rl_digit_loop ();
  1215. }
  1216.  
  1217. /* What to do when you abort reading an argument. */
  1218. rl_discard_argument ()
  1219. {
  1220.   ding ();
  1221.   rl_clear_message ();
  1222.   rl_init_argument ();
  1223. }
  1224.  
  1225. /* Create a default argument. */
  1226. rl_init_argument ()
  1227. {
  1228.   rl_numeric_arg = rl_arg_sign = 1;
  1229.   rl_explicit_arg = 0;
  1230. }
  1231.  
  1232. /* C-u, universal argument.  Multiply the current argument by 4.
  1233.    Read a key.  If the key has nothing to do with arguments, then
  1234.    dispatch on it.  If the key is the abort character then abort. */
  1235. rl_universal_argument ()
  1236. {
  1237.   rl_numeric_arg *= 4;
  1238.   rl_digit_loop ();
  1239. }
  1240.  
  1241. rl_digit_loop ()
  1242. {
  1243.   int key, c;
  1244.   while (1)
  1245.     {
  1246.       rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
  1247.       key = c = rl_read_key ();
  1248.  
  1249.       if (keymap[c].type == ISFUNC &&
  1250.       keymap[c].function == rl_universal_argument)
  1251.     {
  1252.       rl_numeric_arg *= 4;
  1253.       continue;
  1254.     }
  1255.       c = UNMETA (c);
  1256.       if (numeric (c))
  1257.     {
  1258.       if (rl_explicit_arg)
  1259.         rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
  1260.       else
  1261.         rl_numeric_arg = (c - '0');
  1262.       rl_explicit_arg = 1;
  1263.     }
  1264.       else
  1265.     {
  1266.       if (c == '-' && !rl_explicit_arg)
  1267.         {
  1268.           rl_numeric_arg = 1;
  1269.           rl_arg_sign = -1;
  1270.         }
  1271.       else
  1272.         {
  1273.           rl_clear_message ();
  1274.           rl_dispatch (key, keymap);
  1275.           return;
  1276.         }
  1277.     }
  1278.     }
  1279. }
  1280.  
  1281.  
  1282. /* **************************************************************** */
  1283. /*                                    */
  1284. /*            Display stuff                    */
  1285. /*                                    */
  1286. /* **************************************************************** */
  1287.  
  1288. /* This is the stuff that is hard for me.  I never seem to write good
  1289.    display routines in C.  Let's see how I do this time. */
  1290.  
  1291. /* (PWP) Well... Good for a simple line updater, but totally ignores
  1292.    the problems of input lines longer than the screen width.
  1293.  
  1294.    update_line and the code that calls it makes a multiple line,
  1295.    automatically wrapping line update.  Carefull attention needs
  1296.    to be paid to the vertical position variables.
  1297.  
  1298.    handling of terminals with autowrap on (incl. DEC braindamage)
  1299.    could be improved a bit.  Right now I just cheat and decrement
  1300.    screenwidth by one. */
  1301.  
  1302. /* Keep two buffers; one which reflects the current contents of the
  1303.    screen, and the other to draw what we think the new contents should
  1304.    be.  Then compare the buffers, and make whatever changes to the
  1305.    screen itself that we should.  Finally, make the buffer that we
  1306.    just drew into be the one which reflects the current contents of the
  1307.    screen, and place the cursor where it belongs.
  1308.  
  1309.    Commands that want to can fix the display themselves, and then let
  1310.    this function know that the display has been fixed by setting the
  1311.    RL_DISPLAY_FIXED variable.  This is good for efficiency. */
  1312.  
  1313. /* Termcap variables: */
  1314. extern char *term_up, *term_dc, *term_cr;
  1315. extern int screenheight, screenwidth, terminal_can_insert;
  1316.  
  1317. /* What YOU turn on when you have handled all redisplay yourself. */
  1318. int rl_display_fixed = 0;
  1319.  
  1320. /* The visible cursor position.  If you print some text, adjust this. */
  1321. int last_c_pos = 0;
  1322. int last_v_pos = 0;
  1323.  
  1324. /* The last left edge of text that was displayed.  This is used when
  1325.    doing horizontal scrolling.  It shifts in thirds of a screenwidth. */
  1326. static int last_lmargin = 0;
  1327.  
  1328. /* The line display buffers.  One is the line currently displayed on
  1329.    the screen.  The other is the line about to be displayed. */
  1330. static char *visible_line = (char *)NULL;
  1331. static char *invisible_line = (char *)NULL;
  1332.  
  1333. /* Number of lines currently on screen minus 1. */
  1334. int vis_botlin = 0;
  1335.  
  1336. /* A buffer for `modeline' messages. */
  1337. char msg_buf[128];
  1338.  
  1339. /* Non-zero forces the redisplay even if we thought it was unnecessary. */
  1340. int forced_display = 0;
  1341.  
  1342. /* The stuff that gets printed out before the actual text of the line.
  1343.    This is usually pointing to rl_prompt. */
  1344. char *rl_display_prompt = (char *)NULL;
  1345.  
  1346. /* Default and initial buffer size.  Can grow. */
  1347. static int line_size = 1024;
  1348.  
  1349. /* Non-zero means to always use horizontal scrolling in line display. */
  1350. static int horizontal_scroll_mode = 0;
  1351.  
  1352. /* Non-zero means to display an asterisk at the starts of history lines
  1353.    which have been modified. */
  1354. static int mark_modified_lines = 0;
  1355.  
  1356. /* Non-zero means to use a visible bell if one is available rather than
  1357.    simply ringing the terminal bell. */
  1358. static int prefer_visible_bell = 0;
  1359.  
  1360. /* I really disagree with this, but my boss (among others) insists that we
  1361.    support compilers that don't work.  I don't think we are gaining by doing
  1362.    so; what is the advantage in producing better code if we can't use it? */
  1363. /* The following two declarations belong inside the
  1364.    function block, not here. */
  1365. static void move_cursor_relative ();
  1366. static void output_some_chars ();
  1367. static void output_character_function ();
  1368. static int compare_strings ();
  1369.  
  1370. /* Basic redisplay algorithm. */
  1371. rl_redisplay ()
  1372. {
  1373.   register int in, out, c, linenum;
  1374.   register char *line = invisible_line;
  1375.   char *prompt_this_line;
  1376.   int c_pos = 0;
  1377.   int inv_botlin = 0;        /* Number of lines in newly drawn buffer. */
  1378.  
  1379.   extern int readline_echoing_p;
  1380.  
  1381.   if (!readline_echoing_p)
  1382.     return;
  1383.  
  1384.   if (!rl_display_prompt)
  1385.     rl_display_prompt = "";
  1386.  
  1387.   if (!invisible_line)
  1388.     {
  1389.       visible_line = (char *)xmalloc (line_size);
  1390.       invisible_line = (char *)xmalloc (line_size);
  1391.       line = invisible_line;
  1392.       for (in = 0; in < line_size; in++)
  1393.     {
  1394.       visible_line[in] = 0;
  1395.       invisible_line[in] = 1;
  1396.     }
  1397.       rl_on_new_line ();
  1398.     }
  1399.  
  1400.   /* Draw the line into the buffer. */
  1401.   c_pos = -1;
  1402.  
  1403.   /* Mark the line as modified or not.  We only do this for history
  1404.      lines. */
  1405.   out = 0;
  1406.   if (mark_modified_lines && current_history () && rl_undo_list)
  1407.     {
  1408.       line[out++] = '*';
  1409.       line[out] = '\0';
  1410.     }
  1411.  
  1412.   /* If someone thought that the redisplay was handled, but the currently
  1413.      visible line has a different modification state than the one about
  1414.      to become visible, then correct the callers misconception. */
  1415.   if (visible_line[0] != invisible_line[0])
  1416.     rl_display_fixed = 0;
  1417.  
  1418.   prompt_this_line = rindex (rl_display_prompt, '\n');
  1419.   if (!prompt_this_line)
  1420.     prompt_this_line = rl_display_prompt;
  1421.   else
  1422.     {
  1423.       prompt_this_line++;
  1424.       if (forced_display)
  1425.     output_some_chars (rl_display_prompt,
  1426.                prompt_this_line - rl_display_prompt);
  1427.     }
  1428.  
  1429.   strncpy (line + out,  prompt_this_line, strlen (prompt_this_line));
  1430.   out += strlen (prompt_this_line);
  1431.   line[out] = '\0';
  1432.  
  1433.   for (in = 0; in < rl_end; in++)
  1434.     {
  1435.       c = (unsigned char)the_line[in];
  1436.  
  1437.       if (out + 1 >= line_size)
  1438.     {
  1439.       line_size *= 2;
  1440.       visible_line = (char *)xrealloc (visible_line, line_size);
  1441.       invisible_line = (char *)xrealloc (invisible_line, line_size);
  1442.       line = invisible_line;
  1443.     }
  1444.  
  1445.       if (in == rl_point)
  1446.     c_pos = out;
  1447.  
  1448.       if (c > 127)
  1449.     {
  1450.       line[out++] = 'M';
  1451.       line[out++] = '-';
  1452.       line[out++] = c - 128;
  1453.     }
  1454. #define DISPLAY_TABS
  1455. #if defined (DISPLAY_TABS)
  1456.       else if (c == '\t')
  1457.     {
  1458.       register int newout = (out | (int)7) + 1;
  1459.       while (out < newout)
  1460.         line[out++] = ' ';
  1461.     }
  1462. #endif
  1463.       else if (c < 32)
  1464.     {
  1465.       line[out++] = 'C';
  1466.       line[out++] = '-';
  1467.       line[out++] = c + 64;
  1468.     }
  1469.       else if (c == 127)
  1470.     {
  1471.       line[out++] = 'C';
  1472.       line[out++] = '-';
  1473.       line[out++] = '?';
  1474.     }
  1475.       else
  1476.     line[out++] = c;
  1477.     }
  1478.   line[out] = '\0';
  1479.   if (c_pos < 0)
  1480.     c_pos = out;
  1481.  
  1482.   /* PWP: now is when things get a bit hairy.  The visible and invisible
  1483.      line buffers are really multiple lines, which would wrap every
  1484.      (screenwidth - 1) characters.  Go through each in turn, finding
  1485.      the changed region and updating it.  The line order is top to bottom. */
  1486.  
  1487.   /* If we can move the cursor up and down, then use multiple lines,
  1488.      otherwise, let long lines display in a single terminal line, and
  1489.      horizontally scroll it. */
  1490.  
  1491.   if (!horizontal_scroll_mode && term_up && *term_up)
  1492.     {
  1493.       int total_screen_chars = (screenwidth * screenheight);
  1494.  
  1495.       if (!rl_display_fixed || forced_display)
  1496.     {
  1497.       forced_display = 0;
  1498.  
  1499.       /* If we have more than a screenful of material to display, then
  1500.          only display a screenful.  We should display the last screen,
  1501.          not the first.  I'll fix this in a minute. */
  1502.       if (out >= total_screen_chars)
  1503.         out = total_screen_chars - 1;
  1504.  
  1505.       /* Number of screen lines to display. */
  1506.       inv_botlin = out / screenwidth;
  1507.  
  1508.       /* For each line in the buffer, do the updating display. */
  1509.       for (linenum = 0; linenum <= inv_botlin; linenum++)
  1510.         update_line (linenum > vis_botlin ? ""
  1511.              : &visible_line[linenum * screenwidth],
  1512.              &invisible_line[linenum * screenwidth],
  1513.              linenum);
  1514.  
  1515.       /* We may have deleted some lines.  If so, clear the left over
  1516.          blank ones at the bottom out. */
  1517.       if (vis_botlin > inv_botlin)
  1518.         {
  1519.           char *tt;
  1520.           for (; linenum <= vis_botlin; linenum++)
  1521.         {
  1522.           tt = &visible_line[linenum * screenwidth];
  1523.           move_vert (linenum);
  1524.           move_cursor_relative (0, tt);
  1525.           clear_to_eol ((linenum == vis_botlin)?
  1526.                 strlen (tt) : screenwidth);
  1527.         }
  1528.         }
  1529.       vis_botlin = inv_botlin;
  1530.  
  1531.       /* Move the cursor where it should be. */
  1532.       move_vert (c_pos / screenwidth);
  1533.       move_cursor_relative (c_pos % screenwidth,
  1534.                 &invisible_line[(c_pos / screenwidth) * screenwidth]);
  1535.     }
  1536.     }
  1537.   else                /* Do horizontal scrolling. */
  1538.     {
  1539.       int lmargin;
  1540.  
  1541.       /* Always at top line. */
  1542.       last_v_pos = 0;
  1543.  
  1544.       /* If the display position of the cursor would be off the edge
  1545.      of the screen, start the display of this line at an offset that
  1546.      leaves the cursor on the screen. */
  1547.       if (c_pos - last_lmargin > screenwidth - 2)
  1548.     lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3);
  1549.       else if (c_pos - last_lmargin < 1)
  1550.     lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3);
  1551.       else
  1552.     lmargin = last_lmargin;
  1553.  
  1554.       /* If the first character on the screen isn't the first character
  1555.      in the display line, indicate this with a special character. */
  1556.       if (lmargin > 0)
  1557.     line[lmargin] = '<';
  1558.  
  1559.       if (lmargin + screenwidth < out)
  1560.     line[lmargin + screenwidth - 1] = '>';
  1561.  
  1562.       if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
  1563.     {
  1564.       forced_display = 0;
  1565.       update_line (&visible_line[last_lmargin],
  1566.                &invisible_line[lmargin], 0);
  1567.  
  1568.       move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
  1569.       last_lmargin = lmargin;
  1570.     }
  1571.     }
  1572.   fflush (out_stream);
  1573.  
  1574.   /* Swap visible and non-visible lines. */
  1575.   {
  1576.     char *temp = visible_line;
  1577.     visible_line = invisible_line;
  1578.     invisible_line = temp;
  1579.     rl_display_fixed = 0;
  1580.   }
  1581. }
  1582.  
  1583. /* PWP: update_line() is based on finding the middle difference of each
  1584.    line on the screen; vis:
  1585.  
  1586.                  /old first difference
  1587.     /beginning of line   |              /old last same       /old EOL
  1588.     v             v              v                    v
  1589. old:    eddie> Oh, my little gruntle-buggy is to me, as lurgid as
  1590. new:    eddie> Oh, my little buggy says to me, as lurgid as
  1591.     ^             ^        ^               ^
  1592.     \beginning of line   |        \new last same       \new end of line
  1593.                  \new first difference
  1594.  
  1595.    All are character pointers for the sake of speed.  Special cases for
  1596.    no differences, as well as for end of line additions must be handeled.
  1597.  
  1598.    Could be made even smarter, but this works well enough */
  1599. static
  1600. update_line (old, new, current_line)
  1601.      register char *old, *new;
  1602.      int current_line;
  1603. {
  1604.   register char *ofd, *ols, *oe, *nfd, *nls, *ne;
  1605.   int lendiff, wsatend;
  1606.  
  1607.   /* Find first difference. */
  1608.   for (ofd = old, nfd = new;
  1609.        (ofd - old < screenwidth) && *ofd && (*ofd == *nfd);
  1610.        ofd++, nfd++)
  1611.     ;
  1612.  
  1613.   /* Move to the end of the screen line. */
  1614.   for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++);
  1615.   for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++);
  1616.  
  1617.   /* If no difference, continue to next line. */
  1618.   if (ofd == oe && nfd == ne)
  1619.     return;
  1620.  
  1621.   wsatend = 1;            /* flag for trailing whitespace */
  1622.   ols = oe - 1;            /* find last same */
  1623.   nls = ne - 1;
  1624.   while ((*ols == *nls) && (ols > ofd) && (nls > nfd))
  1625.     {
  1626.       if (*ols != ' ')
  1627.     wsatend = 0;
  1628.       ols--;
  1629.       nls--;
  1630.     }
  1631.  
  1632.   if (wsatend)
  1633.     {
  1634.       ols = oe;
  1635.       nls = ne;
  1636.     }
  1637.   else if (*ols != *nls)
  1638.     {
  1639.       if (*ols)            /* don't step past the NUL */
  1640.     ols++;
  1641.       if (*nls)
  1642.     nls++;
  1643.     }
  1644.  
  1645.   move_vert (current_line);
  1646.   move_cursor_relative (ofd - old, old);
  1647.  
  1648.   /* if (len (new) > len (old)) */
  1649.   lendiff = (nls - nfd) - (ols - ofd);
  1650.  
  1651.   /* Insert (diff(len(old),len(new)) ch */
  1652.   if (lendiff > 0)
  1653.     {
  1654.       if (terminal_can_insert)
  1655.     {
  1656.       extern char *term_IC;
  1657.  
  1658.       /* Sometimes it is cheaper to print the characters rather than
  1659.          use the terminal's capabilities. */
  1660.       if ((2 * (ne - nfd)) < lendiff && !term_IC)
  1661.         {
  1662.           output_some_chars (nfd, (ne - nfd));
  1663.           last_c_pos += (ne - nfd);
  1664.         }
  1665.       else
  1666.         {
  1667.           if (*ols)
  1668.         {
  1669.           insert_some_chars (nfd, lendiff);
  1670.           last_c_pos += lendiff;
  1671.         }
  1672.           else
  1673.         {
  1674.           /* At the end of a line the characters do not have to
  1675.              be "inserted".  They can just be placed on the screen. */
  1676.           output_some_chars (nfd, lendiff);
  1677.           last_c_pos += lendiff;
  1678.         }
  1679.           /* Copy (new) chars to screen from first diff to last match. */
  1680.           if (((nls - nfd) - lendiff) > 0)
  1681.         {
  1682.           output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff));
  1683.           last_c_pos += ((nls - nfd) - lendiff);
  1684.         }
  1685.         }
  1686.     }
  1687.       else
  1688.     {        /* cannot insert chars, write to EOL */
  1689.       output_some_chars (nfd, (ne - nfd));
  1690.       last_c_pos += (ne - nfd);
  1691.     }
  1692.     }
  1693.   else                /* Delete characters from line. */
  1694.     {
  1695.       /* If possible and inexpensive to use terminal deletion, then do so. */
  1696.       if (term_dc && (2 * (ne - nfd)) >= (-lendiff))
  1697.     {
  1698.       if (lendiff)
  1699.         delete_chars (-lendiff); /* delete (diff) characters */
  1700.  
  1701.       /* Copy (new) chars to screen from first diff to last match */
  1702.       if ((nls - nfd) > 0)
  1703.         {
  1704.           output_some_chars (nfd, (nls - nfd));
  1705.           last_c_pos += (nls - nfd);
  1706.         }
  1707.     }
  1708.       /* Otherwise, print over the existing material. */
  1709.       else
  1710.     {
  1711.       output_some_chars (nfd, (ne - nfd));
  1712.       last_c_pos += (ne - nfd);
  1713.       clear_to_eol ((oe - old) - (ne - new));
  1714.     }
  1715.     }
  1716. }
  1717.  
  1718. /* (PWP) tell the update routines that we have moved onto a
  1719.    new (empty) line. */
  1720. rl_on_new_line ()
  1721. {
  1722.   if (visible_line)
  1723.     visible_line[0] = '\0';
  1724.  
  1725.   last_c_pos = last_v_pos = 0;
  1726.   vis_botlin = last_lmargin = 0;
  1727. }
  1728.  
  1729. /* Actually update the display, period. */
  1730. rl_forced_update_display ()
  1731. {
  1732.   if (visible_line)
  1733.     {
  1734.       register char *temp = visible_line;
  1735.  
  1736.       while (*temp) *temp++ = '\0';
  1737.     }
  1738.   rl_on_new_line ();
  1739.   forced_display++;
  1740.   rl_redisplay ();
  1741. }
  1742.  
  1743. /* Move the cursor from last_c_pos to NEW, which are buffer indices.
  1744.    DATA is the contents of the screen line of interest; i.e., where
  1745.    the movement is being done. */
  1746. static void
  1747. move_cursor_relative (new, data)
  1748.      int new;
  1749.      char *data;
  1750. {
  1751.   register int i;
  1752.  
  1753.   /* It may be faster to output a CR, and then move forwards instead
  1754.      of moving backwards. */
  1755.   if (new + 1 < last_c_pos - new)
  1756.     {
  1757.       tputs (term_cr, 1, output_character_function);
  1758.       last_c_pos = 0;
  1759.     }
  1760.  
  1761.   if (last_c_pos == new) return;
  1762.  
  1763.   if (last_c_pos < new)
  1764.     {
  1765.       /* Move the cursor forward.  We do it by printing the command
  1766.      to move the cursor forward if there is one, else print that
  1767.      portion of the output buffer again.  Which is cheaper? */
  1768.  
  1769.       /* The above comment is left here for posterity.  It is faster
  1770.      to print one character (non-control) than to print a control
  1771.      sequence telling the terminal to move forward one character.
  1772.      That kind of control is for people who don't know what the
  1773.      data is underneath the cursor. */
  1774. #if defined (HACK_TERMCAP_MOTION)
  1775.       extern char *term_forward_char;
  1776.  
  1777.       if (term_forward_char)
  1778.     for (i = last_c_pos; i < new; i++)
  1779.       tputs (term_forward_char, 1, output_character_function);
  1780.       else
  1781.     for (i = last_c_pos; i < new; i++)
  1782.       putc (data[i], out_stream);
  1783. #else
  1784.       for (i = last_c_pos; i < new; i++)
  1785.     putc (data[i], out_stream);
  1786. #endif                /* HACK_TERMCAP_MOTION */
  1787.     }
  1788.   else
  1789.     backspace (last_c_pos - new);
  1790.   last_c_pos = new;
  1791. }
  1792.  
  1793. /* PWP: move the cursor up or down. */
  1794. move_vert (to)
  1795.      int to;
  1796. {
  1797.   void output_character_function ();
  1798.   register int delta, i;
  1799.  
  1800.   if (last_v_pos == to) return;
  1801.  
  1802.   if (to > screenheight)
  1803.     return;
  1804.  
  1805.   if ((delta = to - last_v_pos) > 0)
  1806.     {
  1807.       for (i = 0; i < delta; i++)
  1808.     putc ('\n', out_stream);
  1809.       tputs (term_cr, 1, output_character_function);
  1810.       last_c_pos = 0;
  1811.     }
  1812.   else
  1813.     {            /* delta < 0 */
  1814.       if (term_up && *term_up)
  1815.     for (i = 0; i < -delta; i++)
  1816.       tputs (term_up, 1, output_character_function);
  1817.     }
  1818.   last_v_pos = to;        /* now to is here */
  1819. }
  1820.  
  1821. /* Physically print C on out_stream.  This is for functions which know
  1822.    how to optimize the display. */
  1823. rl_show_char (c)
  1824.      int c;
  1825. {
  1826.   if (c > 127)
  1827.     {
  1828.       fprintf (out_stream, "M-");
  1829.       c -= 128;
  1830.     }
  1831.  
  1832. #if defined (DISPLAY_TABS)
  1833.   if (c < 32 && c != '\t')
  1834. #else
  1835.   if (c < 32)
  1836. #endif
  1837.     {
  1838.  
  1839.       c += 64;
  1840.     }
  1841.  
  1842.   putc (c, out_stream);
  1843.   fflush (out_stream);
  1844. }
  1845.  
  1846. #if defined (DISPLAY_TABS)
  1847. int
  1848. rl_character_len (c, pos)
  1849.      register int c, pos;
  1850. {
  1851.   if (c < ' ' || c > 126)
  1852.     {
  1853.       if (c == '\t')
  1854.     return (((pos | (int)7) + 1) - pos);
  1855.       else
  1856.     return (3);
  1857.     }
  1858.   else
  1859.     return (1);
  1860. }
  1861. #else
  1862. int
  1863. rl_character_len (c)
  1864.      int c;
  1865. {
  1866.   if (c < ' ' || c > 126)
  1867.     return (3);
  1868.   else
  1869.     return (1);
  1870. }
  1871. #endif  /* DISPLAY_TAB */
  1872.  
  1873. /* How to print things in the "echo-area".  The prompt is treated as a
  1874.    mini-modeline. */
  1875. rl_message (string, arg1, arg2)
  1876.      char *string;
  1877. {
  1878.   sprintf (msg_buf, string, arg1, arg2);
  1879.   rl_display_prompt = msg_buf;
  1880.   rl_redisplay ();
  1881. }
  1882.  
  1883. /* How to clear things from the "echo-area". */
  1884. rl_clear_message ()
  1885. {
  1886.   rl_display_prompt = rl_prompt;
  1887.   rl_redisplay ();
  1888. }
  1889.  
  1890. /* **************************************************************** */
  1891. /*                                    */
  1892. /*            Terminal and Termcap                */
  1893. /*                                    */
  1894. /* **************************************************************** */
  1895.  
  1896. static char *term_buffer = (char *)NULL;
  1897. static char *term_string_buffer = (char *)NULL;
  1898.  
  1899. /* Non-zero means this terminal can't really do anything. */
  1900. int dumb_term = 0;
  1901.  
  1902. char PC;
  1903. char *BC, *UP;
  1904.  
  1905. /* Some strings to control terminal actions.  These are output by tputs (). */
  1906. char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
  1907.  
  1908. int screenwidth, screenheight;
  1909.  
  1910. /* Non-zero if we determine that the terminal can do character insertion. */
  1911. int terminal_can_insert = 0;
  1912.  
  1913. /* How to insert characters. */
  1914. char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
  1915.  
  1916. /* How to delete characters. */
  1917. char *term_dc, *term_DC;
  1918.  
  1919. #if defined (HACK_TERMCAP_MOTION)
  1920. char *term_forward_char;
  1921. #endif  /* HACK_TERMCAP_MOTION */
  1922.  
  1923. /* How to go up a line. */
  1924. char *term_up;
  1925.  
  1926. /* A visible bell, if the terminal can be made to flash the screen. */
  1927. char *visible_bell;
  1928.  
  1929. /* Re-initialize the terminal considering that the TERM/TERMCAP variable
  1930.    has changed. */
  1931. rl_reset_terminal (terminal_name)
  1932.      char *terminal_name;
  1933. {
  1934.   init_terminal_io (terminal_name);
  1935. }
  1936.  
  1937. init_terminal_io (terminal_name)
  1938.      char *terminal_name;
  1939. {
  1940.   extern char *tgetstr ();
  1941.   char *term, *buffer;
  1942. #if defined (TIOCGWINSZ)
  1943.   struct winsize window_size;
  1944. #endif
  1945.   int tty;
  1946.  
  1947.   term = terminal_name ? terminal_name : getenv ("TERM");
  1948.  
  1949.   if (!term_string_buffer)
  1950.     term_string_buffer = (char *)xmalloc (2048);
  1951.  
  1952.   if (!term_buffer)
  1953.     term_buffer = (char *)xmalloc (2048);
  1954.  
  1955.   buffer = term_string_buffer;
  1956.  
  1957.   term_clrpag = term_cr = term_clreol = (char *)NULL;
  1958.  
  1959.   if (!term)
  1960.     term = "dumb";
  1961.  
  1962.   if (tgetent (term_buffer, term) < 0)
  1963.     {
  1964.       dumb_term = 1;
  1965.       screenwidth = 79;
  1966.       screenheight = 24;
  1967.       term_cr = "\r";
  1968.       term_im = term_ei = term_ic = term_IC = (char *)NULL;
  1969.       term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  1970. #if defined (HACK_TERMCAP_MOTION)
  1971.       term_forward_char = (char *)NULL;
  1972. #endif
  1973.       terminal_can_insert = 0;
  1974.       return;
  1975.     }
  1976.  
  1977.   BC = tgetstr ("pc", &buffer);
  1978.   PC = buffer ? *buffer : 0;
  1979.  
  1980.   term_backspace = tgetstr ("le", &buffer);
  1981.  
  1982.   term_cr = tgetstr ("cr", &buffer);
  1983.   term_clreol = tgetstr ("ce", &buffer);
  1984.   term_clrpag = tgetstr ("cl", &buffer);
  1985.  
  1986.   if (!term_cr)
  1987.     term_cr =  "\r";
  1988.  
  1989. #if defined (HACK_TERMCAP_MOTION)
  1990.   term_forward_char = tgetstr ("nd", &buffer);
  1991. #endif  /* HACK_TERMCAP_MOTION */
  1992.  
  1993.   if (rl_instream)
  1994.     tty = fileno (rl_instream);
  1995.   else
  1996.     tty = 0;
  1997.  
  1998.   screenwidth = screenheight = 0;
  1999. #if defined (TIOCGWINSZ)
  2000.   if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
  2001.     {
  2002.       screenwidth = (int) window_size.ws_col;
  2003.       screenheight = (int) window_size.ws_row;
  2004.     }
  2005. #endif
  2006.  
  2007.   if (screenwidth <= 0 || screenheight <= 0)
  2008.     {
  2009.       screenwidth = tgetnum ("co");
  2010.       screenheight = tgetnum ("li");
  2011.     }
  2012.  
  2013.   screenwidth--;
  2014.  
  2015.   if (screenwidth <= 0)
  2016.     screenwidth = 79;
  2017.  
  2018.   if (screenheight <= 0)
  2019.     screenheight = 24;
  2020.  
  2021.   term_im = tgetstr ("im", &buffer);
  2022.   term_ei = tgetstr ("ei", &buffer);
  2023.   term_IC = tgetstr ("IC", &buffer);
  2024.   term_ic = tgetstr ("ic", &buffer);
  2025.  
  2026.   /* "An application program can assume that the terminal can do
  2027.       character insertion if *any one of* the capabilities `IC',
  2028.       `im', `ic' or `ip' is provided."  But we can't do anything if
  2029.       only `ip' is provided, so... */
  2030.   terminal_can_insert = (term_IC || term_im || term_ic);
  2031.  
  2032.   term_up = tgetstr ("up", &buffer);
  2033.   term_dc = tgetstr ("dc", &buffer);
  2034.   term_DC = tgetstr ("DC", &buffer);
  2035.  
  2036.   visible_bell = tgetstr ("vb", &buffer);
  2037. }
  2038.  
  2039. /* A function for the use of tputs () */
  2040. static void
  2041. output_character_function (c)
  2042.      int c;
  2043. {
  2044.   putc (c, out_stream);
  2045. }
  2046.  
  2047. /* Write COUNT characters from STRING to the output stream. */
  2048. static void
  2049. output_some_chars (string, count)
  2050.      char *string;
  2051.      int count;
  2052. {
  2053.   fwrite (string, 1, count, out_stream);
  2054. }
  2055.  
  2056. /* Delete COUNT characters from the display line. */
  2057. static
  2058. delete_chars (count)
  2059.      int count;
  2060. {
  2061.   if (count > screenwidth)
  2062.     return;
  2063.  
  2064.   if (term_DC && *term_DC)
  2065.     {
  2066.       char *tgoto (), *buffer;
  2067.       buffer = tgoto (term_DC, 0, count);
  2068.       tputs (buffer, 1, output_character_function);
  2069.     }
  2070.   else
  2071.     {
  2072.       if (term_dc && *term_dc)
  2073.     while (count--)
  2074.       tputs (term_dc, 1, output_character_function);
  2075.     }
  2076. }
  2077.  
  2078. /* Insert COUNT characters from STRING to the output stream. */
  2079. static
  2080. insert_some_chars (string, count)
  2081.      char *string;
  2082.      int count;
  2083. {
  2084.   /* If IC is defined, then we do not have to "enter" insert mode. */
  2085.   if (term_IC)
  2086.     {
  2087.       char *tgoto (), *buffer;
  2088.       buffer = tgoto (term_IC, 0, count);
  2089.       tputs (buffer, 1, output_character_function);
  2090.       output_some_chars (string, count);
  2091.     }
  2092.   else
  2093.     {
  2094.       register int i;
  2095.  
  2096.       /* If we have to turn on insert-mode, then do so. */
  2097.       if (term_im && *term_im)
  2098.     tputs (term_im, 1, output_character_function);
  2099.  
  2100.       /* If there is a special command for inserting characters, then
  2101.      use that first to open up the space. */
  2102.       if (term_ic && *term_ic)
  2103.     {
  2104.       for (i = count; i--; )
  2105.         tputs (term_ic, 1, output_character_function);
  2106.     }
  2107.  
  2108.       /* Print the text. */
  2109.       output_some_chars (string, count);
  2110.  
  2111.       /* If there is a string to turn off insert mode, we had best use
  2112.      it now. */
  2113.       if (term_ei && *term_ei)
  2114.     tputs (term_ei, 1, output_character_function);
  2115.     }
  2116. }
  2117.  
  2118. /* Move the cursor back. */
  2119. backspace (count)
  2120.      int count;
  2121. {
  2122.   register int i;
  2123.  
  2124.   if (term_backspace)
  2125.     for (i = 0; i < count; i++)
  2126.       tputs (term_backspace, 1, output_character_function);
  2127.   else
  2128.     for (i = 0; i < count; i++)
  2129.       putc ('\b', out_stream);
  2130. }
  2131.  
  2132. /* Move to the start of the next line. */
  2133. crlf ()
  2134. {
  2135. #if defined (NEW_TTY_DRIVER)
  2136.   tputs (term_cr, 1, output_character_function);
  2137. #endif /* NEW_TTY_DRIVER */
  2138.   putc ('\n', out_stream);
  2139. }
  2140.  
  2141. /* Clear to the end of the line.  COUNT is the minimum
  2142.    number of character spaces to clear, */
  2143. clear_to_eol (count)
  2144.      int count;
  2145. {
  2146.   if (term_clreol)
  2147.     {
  2148.       tputs (term_clreol, 1, output_character_function);
  2149.     }
  2150.   else
  2151.     {
  2152.       register int i;
  2153.  
  2154.       /* Do one more character space. */
  2155.       count++;
  2156.  
  2157.       for (i = 0; i < count; i++)
  2158.     putc (' ', out_stream);
  2159.  
  2160.       backspace (count);
  2161.     }
  2162. }
  2163.  
  2164.  
  2165. /* **************************************************************** */
  2166. /*                                    */
  2167. /*              Saving and Restoring the TTY                */
  2168. /*                                    */
  2169. /* **************************************************************** */
  2170.  
  2171. /* Non-zero means that the terminal is in a prepped state. */
  2172. static int terminal_prepped = 0;
  2173.  
  2174. #if defined (NEW_TTY_DRIVER)
  2175.  
  2176. /* Standard flags, including ECHO. */
  2177. static int original_tty_flags = 0;
  2178.  
  2179. /* Local mode flags, like LPASS8. */
  2180. static int local_mode_flags = 0;
  2181.  
  2182. /* Terminal characters.  This has C-s and C-q in it. */
  2183. static struct tchars original_tchars;
  2184.  
  2185. /* Local special characters.  This has the interrupt characters in it. */
  2186. #if defined (TIOCGLTC)
  2187. static struct ltchars original_ltchars;
  2188. #endif
  2189.  
  2190. /* We use this to get and set the tty_flags. */
  2191. static struct sgttyb the_ttybuff;
  2192.  
  2193. /* Put the terminal in CBREAK mode so that we can detect key presses. */
  2194. static void
  2195. rl_prep_terminal ()
  2196. {
  2197.   int tty = fileno (rl_instream);
  2198. #if defined (HAVE_BSD_SIGNALS)
  2199.   int oldmask;
  2200. #endif /* HAVE_BSD_SIGNALS */
  2201.  
  2202.   if (terminal_prepped)
  2203.     return;
  2204.  
  2205.   oldmask = sigblock (sigmask (SIGINT));
  2206.  
  2207.   /* We always get the latest tty values.  Maybe stty changed them. */
  2208.   ioctl (tty, TIOCGETP, &the_ttybuff);
  2209.   original_tty_flags = the_ttybuff.sg_flags;
  2210.  
  2211.   readline_echoing_p = (original_tty_flags & ECHO);
  2212.  
  2213. #if defined (TIOCLGET)
  2214.   ioctl (tty, TIOCLGET, &local_mode_flags);
  2215. #endif
  2216.  
  2217. #if !defined (ANYP)
  2218. #  define ANYP (EVENP | ODDP)
  2219. #endif
  2220.  
  2221.   /* If this terminal doesn't care how the 8th bit is used,
  2222.      then we can use it for the meta-key.  We check by seeing
  2223.      if BOTH odd and even parity are allowed. */
  2224.   if (the_ttybuff.sg_flags & ANYP)
  2225.     {
  2226. #if defined (PASS8)
  2227.       the_ttybuff.sg_flags |= PASS8;
  2228. #endif
  2229.  
  2230.       /* Hack on local mode flags if we can. */
  2231. #if defined (TIOCLGET) && defined (LPASS8)
  2232.       {
  2233.     int flags;
  2234.     flags = local_mode_flags | LPASS8;
  2235.     ioctl (tty, TIOCLSET, &flags);
  2236.       }
  2237. #endif /* TIOCLGET && LPASS8 */
  2238.     }
  2239.  
  2240. #if defined (TIOCGETC)
  2241.   {
  2242.     struct tchars temp;
  2243.  
  2244.     ioctl (tty, TIOCGETC, &original_tchars);
  2245.     temp = original_tchars;
  2246.  
  2247. #if defined (USE_XON_XOFF)
  2248.     /* Get rid of C-s and C-q.
  2249.        We remember the value of startc (C-q) so that if the terminal is in
  2250.        xoff state, the user can xon it by pressing that character. */
  2251.     xon_char = temp.t_startc;
  2252.     temp.t_stopc = -1;
  2253.     temp.t_startc = -1;
  2254.  
  2255.     /* If there is an XON character, bind it to restart the output. */
  2256.     if (xon_char != -1)
  2257.       rl_bind_key (xon_char, rl_restart_output);
  2258. #endif /* USE_XON_XOFF */
  2259.  
  2260.     /* If there is an EOF char, bind eof_char to it. */
  2261.     if (temp.t_eofc != -1)
  2262.       eof_char = temp.t_eofc;
  2263.  
  2264. #if defined (NO_KILL_INTR)
  2265.     /* Get rid of C-\ and C-c. */
  2266.     temp.t_intrc = temp.t_quitc = -1;
  2267. #endif /* NO_KILL_INTR */
  2268.  
  2269.     ioctl (tty, TIOCSETC, &temp);
  2270.   }
  2271. #endif /* TIOCGETC */
  2272.  
  2273. #if defined (TIOCGLTC)
  2274.   {
  2275.     struct ltchars temp;
  2276.  
  2277.     ioctl (tty, TIOCGLTC, &original_ltchars);
  2278.     temp = original_ltchars;
  2279.  
  2280.     /* Make the interrupt keys go away.  Just enough to make people
  2281.        happy. */
  2282.     temp.t_dsuspc = -1;    /* C-y */
  2283.     temp.t_lnextc = -1;    /* C-v */
  2284.  
  2285.     ioctl (tty, TIOCSLTC, &temp);
  2286.   }
  2287. #endif /* TIOCGLTC */
  2288.  
  2289.   the_ttybuff.sg_flags &= ~(ECHO | CRMOD);
  2290.   the_ttybuff.sg_flags |= CBREAK;
  2291.   ioctl (tty, TIOCSETN, &the_ttybuff);
  2292.  
  2293.   terminal_prepped = 1;
  2294.  
  2295. #if defined (HAVE_BSD_SIGNALS)
  2296.   sigsetmask (oldmask);
  2297. #endif
  2298. }
  2299.  
  2300. /* Restore the terminal to its original state. */
  2301. static void
  2302. rl_deprep_terminal ()
  2303. {
  2304.   int tty = fileno (rl_instream);
  2305. #if defined (HAVE_BSD_SIGNALS)
  2306.   int oldmask;
  2307. #endif
  2308.  
  2309.   if (!terminal_prepped)
  2310.     return;
  2311.  
  2312.   oldmask = sigblock (sigmask (SIGINT));
  2313.  
  2314.   the_ttybuff.sg_flags = original_tty_flags;
  2315.   ioctl (tty, TIOCSETN, &the_ttybuff);
  2316.   readline_echoing_p = 1;
  2317.  
  2318. #if defined (TIOCLGET)
  2319.   ioctl (tty, TIOCLSET, &local_mode_flags);
  2320. #endif
  2321.  
  2322. #if defined (TIOCSLTC)
  2323.   ioctl (tty, TIOCSLTC, &original_ltchars);
  2324. #endif
  2325.  
  2326. #if defined (TIOCSETC)
  2327.   ioctl (tty, TIOCSETC, &original_tchars);
  2328. #endif
  2329.   terminal_prepped = 0;
  2330.  
  2331. #if defined (HAVE_BSD_SIGNALS)
  2332.   sigsetmask (oldmask);
  2333. #endif
  2334. }
  2335.  
  2336. #else  /* !defined (NEW_TTY_DRIVER) */
  2337.  
  2338. #if !defined (VMIN)
  2339. #define VMIN VEOF
  2340. #endif
  2341.  
  2342. #if !defined (VTIME)
  2343. #define VTIME VEOL
  2344. #endif
  2345.  
  2346. #if defined (TERMIOS_TTY_DRIVER)
  2347. static struct termios otio;
  2348. #else
  2349. static struct termio otio;
  2350. #endif /* !TERMIOS_TTY_DRIVER */
  2351.  
  2352. static void
  2353. rl_prep_terminal ()
  2354. {
  2355.   int tty = fileno (rl_instream);
  2356. #if defined (TERMIOS_TTY_DRIVER)
  2357.   struct termios tio;
  2358. #else
  2359.   struct termio tio;
  2360. #endif /* !TERMIOS_TTY_DRIVER */
  2361.  
  2362. #if defined (HAVE_POSIX_SIGNALS)
  2363.   sigset_t set, oset;
  2364. #else
  2365. #  if defined (HAVE_BSD_SIGNALS)
  2366.   int oldmask;
  2367. #  endif /* HAVE_BSD_SIGNALS */
  2368. #endif /* !HAVE_POSIX_SIGNALS */
  2369.  
  2370.   if (terminal_prepped)
  2371.     return;
  2372.  
  2373.   /* Try to keep this function from being INTerrupted.  We can do it
  2374.      on POSIX and systems with BSD-like signal handling. */
  2375. #if defined (HAVE_POSIX_SIGNALS)
  2376.   sigemptyset (&set);
  2377.   sigaddset (&set, SIGINT);
  2378.   sigprocmask (SIG_BLOCK, &set, &oset);
  2379. #else /* !HAVE_POSIX_SIGNALS */
  2380. #  if defined (HAVE_BSD_SIGNALS)
  2381.   oldmask = sigblock (sigmask (SIGINT));
  2382. #  endif /* HAVE_BSD_SIGNALS */
  2383. #endif /* !HAVE_POSIX_SIGNALS */
  2384.  
  2385. #if defined (TERMIOS_TTY_DRIVER)
  2386.   tcgetattr (tty, &tio);
  2387. #else
  2388.   ioctl (tty, TCGETA, &tio);
  2389. #endif /* !TERMIOS_TTY_DRIVER */
  2390.  
  2391.   otio = tio;
  2392.  
  2393.   readline_echoing_p = (tio.c_lflag & ECHO);
  2394.  
  2395.   tio.c_lflag &= ~(ICANON|ECHO);
  2396.  
  2397. #if defined (USE_XON_XOFF)
  2398. #if defined (IXANY)
  2399.   tio.c_iflag &= ~(IXON|IXOFF|IXANY);
  2400. #else
  2401.   /* `strict' Posix systems do not define IXANY. */
  2402.   tio.c_iflag &= ~(IXON|IXOFF);
  2403. #endif /* IXANY */
  2404. #endif /* USE_XON_XOFF */
  2405.  
  2406.   /* Only turn this off if we are using all 8 bits. */
  2407.   /* |ISTRIP|INPCK */
  2408.   tio.c_iflag &= ~(ISTRIP | INPCK);
  2409.  
  2410.   /* Make sure we differentiate between CR and NL on input. */
  2411.   tio.c_iflag &= ~(ICRNL | INLCR);
  2412.  
  2413. #if !defined (HANDLE_SIGNALS)
  2414.   tio.c_lflag &= ~ISIG;
  2415. #else
  2416.   tio.c_lflag |= ISIG;
  2417. #endif
  2418.  
  2419.   tio.c_cc[VMIN] = 1;
  2420.   tio.c_cc[VTIME] = 0;
  2421.  
  2422.   /* Turn off characters that we need on Posix systems with job control,
  2423.      just to be sure.  This includes ^Y and ^V.  This should not really
  2424.      be necessary.  */
  2425. #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_JOB_CONTROL)
  2426.  
  2427. #if !defined (_POSIX_VDISABLE)
  2428. #define _POSIX_VDISABLE    0
  2429. #endif /* _POSIX_VDISABLE */
  2430.  
  2431. #if defined (VLNEXT)
  2432.   tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
  2433. #endif
  2434.  
  2435. #if defined (VDSUSP)
  2436.   tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
  2437. #endif
  2438.  
  2439. #endif /* POSIX && JOB_CONTROL */
  2440.  
  2441. #if defined (TERMIOS_TTY_DRIVER)
  2442.   tcsetattr (tty, TCSADRAIN, &tio);
  2443.   tcflow (tty, TCOON);        /* Simulate a ^Q. */
  2444. #else
  2445.   ioctl (tty, TCSETAW, &tio);
  2446.   ioctl (tty, TCXONC, 1);    /* Simulate a ^Q. */
  2447. #endif /* !TERMIOS_TTY_DRIVER */
  2448.  
  2449.   terminal_prepped = 1;
  2450.  
  2451. #if defined (HAVE_POSIX_SIGNALS)
  2452.   sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
  2453. #else
  2454. #  if defined (HAVE_BSD_SIGNALS)
  2455.   sigsetmask (oldmask);
  2456. #  endif /* HAVE_BSD_SIGNALS */
  2457. #endif /* !HAVE_POSIX_SIGNALS */
  2458. }
  2459.  
  2460. static void
  2461. rl_deprep_terminal ()
  2462. {
  2463.   int tty = fileno (rl_instream);
  2464.  
  2465.   /* Try to keep this function from being INTerrupted.  We can do it
  2466.      on POSIX and systems with BSD-like signal handling. */
  2467. #if defined (HAVE_POSIX_SIGNALS)
  2468.   sigset_t set, oset;
  2469. #else /* !HAVE_POSIX_SIGNALS */
  2470. #  if defined (HAVE_BSD_SIGNALS)
  2471.   int oldmask;
  2472. #  endif /* HAVE_BSD_SIGNALS */
  2473. #endif /* !HAVE_POSIX_SIGNALS */
  2474.  
  2475.   if (!terminal_prepped)
  2476.     return;
  2477.  
  2478. #if defined (HAVE_POSIX_SIGNALS)
  2479.   sigemptyset (&set);
  2480.   sigaddset (&set, SIGINT);
  2481.   sigprocmask (SIG_BLOCK, &set, &oset);
  2482. #else /* !HAVE_POSIX_SIGNALS */
  2483. #  if defined (HAVE_BSD_SIGNALS)
  2484.   oldmask = sigblock (sigmask (SIGINT));
  2485. #  endif /* HAVE_BSD_SIGNALS */
  2486. #endif /* !HAVE_POSIX_SIGNALS */
  2487.  
  2488. #if defined (TERMIOS_TTY_DRIVER)
  2489.   tcsetattr (tty, TCSADRAIN, &otio);
  2490.   tcflow (tty, TCOON);        /* Simulate a ^Q. */
  2491. #else /* TERMIOS_TTY_DRIVER */
  2492.   ioctl (tty, TCSETAW, &otio);
  2493.   ioctl (tty, TCXONC, 1);    /* Simulate a ^Q. */
  2494. #endif /* !TERMIOS_TTY_DRIVER */
  2495.  
  2496.   terminal_prepped = 0;
  2497.  
  2498. #if defined (HAVE_POSIX_SIGNALS)
  2499.   sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
  2500. #else /* !HAVE_POSIX_SIGNALS */
  2501. #  if defined (HAVE_BSD_SIGNALS)
  2502.   sigsetmask (oldmask);
  2503. #  endif /* HAVE_BSD_SIGNALS */
  2504. #endif /* !HAVE_POSIX_SIGNALS */
  2505. }
  2506. #endif  /* NEW_TTY_DRIVER */
  2507.  
  2508.  
  2509. /* **************************************************************** */
  2510. /*                                    */
  2511. /*            Utility Functions                */
  2512. /*                                    */
  2513. /* **************************************************************** */
  2514.  
  2515. /* Return 0 if C is not a member of the class of characters that belong
  2516.    in words, or 1 if it is. */
  2517.  
  2518. int allow_pathname_alphabetic_chars = 0;
  2519. char *pathname_alphabetic_chars = "/-_=~.#$";
  2520.  
  2521. int
  2522. alphabetic (c)
  2523.      int c;
  2524. {
  2525.   if (pure_alphabetic (c) || (numeric (c)))
  2526.     return (1);
  2527.  
  2528.   if (allow_pathname_alphabetic_chars)
  2529.     return ((int)rindex (pathname_alphabetic_chars, c));
  2530.   else
  2531.     return (0);
  2532. }
  2533.  
  2534. /* Return non-zero if C is a numeric character. */
  2535. int
  2536. numeric (c)
  2537.      int c;
  2538. {
  2539.   return (c >= '0' && c <= '9');
  2540. }
  2541.  
  2542. /* Ring the terminal bell. */
  2543. int
  2544. ding ()
  2545. {
  2546.   if (readline_echoing_p)
  2547.     {
  2548.       if (prefer_visible_bell && visible_bell)
  2549.     tputs (visible_bell, 1, output_character_function);
  2550.       else
  2551.     {
  2552.       fprintf (stderr, "\007");
  2553.       fflush (stderr);
  2554.     }
  2555.     }
  2556.   return (-1);
  2557. }
  2558.  
  2559. /* How to abort things. */
  2560. rl_abort ()
  2561. {
  2562.   ding ();
  2563.   rl_clear_message ();
  2564.   rl_init_argument ();
  2565.   rl_pending_input = 0;
  2566.  
  2567.   defining_kbd_macro = 0;
  2568.   while (executing_macro)
  2569.     pop_executing_macro ();
  2570.  
  2571.   rl_last_func = (Function *)NULL;
  2572.   longjmp (readline_top_level, 1);
  2573. }
  2574.  
  2575. /* Return a copy of the string between FROM and TO.
  2576.    FROM is inclusive, TO is not. */
  2577. #if defined (sun) /* Yes, that's right, some crufty function in sunview is
  2578.              called rl_copy (). */
  2579. static
  2580. #endif
  2581. char *
  2582. rl_copy (from, to)
  2583.      int from, to;
  2584. {
  2585.   register int length;
  2586.   char *copy;
  2587.  
  2588.   /* Fix it if the caller is confused. */
  2589.   if (from > to) {
  2590.     int t = from;
  2591.     from = to;
  2592.     to = t;
  2593.   }
  2594.  
  2595.   length = to - from;
  2596.   copy = (char *)xmalloc (1 + length);
  2597.   strncpy (copy, the_line + from, length);
  2598.   copy[length] = '\0';
  2599.   return (copy);
  2600. }
  2601.  
  2602. /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
  2603.    LEN characters. */
  2604. void
  2605. rl_extend_line_buffer (len)
  2606.      int len;
  2607. {
  2608.   while (len >= rl_line_buffer_len)
  2609.     rl_line_buffer =
  2610.       (char *)xrealloc
  2611.     (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
  2612.  
  2613.   the_line = rl_line_buffer;
  2614. }
  2615.  
  2616.  
  2617. /* **************************************************************** */
  2618. /*                                    */
  2619. /*            Insert and Delete                */
  2620. /*                                    */
  2621. /* **************************************************************** */
  2622.  
  2623. /* Insert a string of text into the line at point.  This is the only
  2624.    way that you should do insertion.  rl_insert () calls this
  2625.    function. */
  2626. rl_insert_text (string)
  2627.      char *string;
  2628. {
  2629.   extern int doing_an_undo;
  2630.   register int i, l = strlen (string);
  2631.  
  2632.   if (rl_end + l >= rl_line_buffer_len)
  2633.     rl_extend_line_buffer (rl_end + l);
  2634.  
  2635.   for (i = rl_end; i >= rl_point; i--)
  2636.     the_line[i + l] = the_line[i];
  2637.   strncpy (the_line + rl_point, string, l);
  2638.  
  2639.   /* Remember how to undo this if we aren't undoing something. */
  2640.   if (!doing_an_undo)
  2641.     {
  2642.       /* If possible and desirable, concatenate the undos. */
  2643.       if ((strlen (string) == 1) &&
  2644.       rl_undo_list &&
  2645.       (rl_undo_list->what == UNDO_INSERT) &&
  2646.       (rl_undo_list->end == rl_point) &&
  2647.       (rl_undo_list->end - rl_undo_list->start < 20))
  2648.     rl_undo_list->end++;
  2649.       else
  2650.     rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
  2651.     }
  2652.   rl_point += l;
  2653.   rl_end += l;
  2654.   the_line[rl_end] = '\0';
  2655. }
  2656.  
  2657. /* Delete the string between FROM and TO.  FROM is
  2658.    inclusive, TO is not. */
  2659. rl_delete_text (from, to)
  2660.      int from, to;
  2661. {
  2662.   extern int doing_an_undo;
  2663.   register char *text;
  2664.  
  2665.   /* Fix it if the caller is confused. */
  2666.   if (from > to)
  2667.     {
  2668.       int t = from;
  2669.       from = to;
  2670.       to = t;
  2671.     }
  2672.   text = rl_copy (from, to);
  2673.   strncpy (the_line + from, the_line + to, rl_end - to);
  2674.  
  2675.   /* Remember how to undo this delete. */
  2676.   if (!doing_an_undo)
  2677.     rl_add_undo (UNDO_DELETE, from, to, text);
  2678.   else
  2679.     free (text);
  2680.  
  2681.   rl_end -= (to - from);
  2682.   the_line[rl_end] = '\0';
  2683. }
  2684.  
  2685.  
  2686. /* **************************************************************** */
  2687. /*                                    */
  2688. /*            Readline character functions            */
  2689. /*                                    */
  2690. /* **************************************************************** */
  2691.  
  2692. /* This is not a gap editor, just a stupid line input routine.  No hair
  2693.    is involved in writing any of the functions, and none should be. */
  2694.  
  2695. /* Note that:
  2696.  
  2697.    rl_end is the place in the string that we would place '\0';
  2698.    i.e., it is always safe to place '\0' there.
  2699.  
  2700.    rl_point is the place in the string where the cursor is.  Sometimes
  2701.    this is the same as rl_end.
  2702.  
  2703.    Any command that is called interactively receives two arguments.
  2704.    The first is a count: the numeric arg pased to this command.
  2705.    The second is the key which invoked this command.
  2706. */
  2707.  
  2708.  
  2709. /* **************************************************************** */
  2710. /*                                    */
  2711. /*            Movement Commands                */
  2712. /*                                    */
  2713. /* **************************************************************** */
  2714.  
  2715. /* Note that if you `optimize' the display for these functions, you cannot
  2716.    use said functions in other functions which do not do optimizing display.
  2717.    I.e., you will have to update the data base for rl_redisplay, and you
  2718.    might as well let rl_redisplay do that job. */
  2719.  
  2720. /* Move forward COUNT characters. */
  2721. rl_forward (count)
  2722.      int count;
  2723. {
  2724.   if (count < 0)
  2725.     rl_backward (-count);
  2726.   else
  2727.     while (count)
  2728.       {
  2729. #if defined (VI_MODE)
  2730.     if (rl_point == (rl_end - (rl_editing_mode == vi_mode)))
  2731. #else
  2732.     if (rl_point == rl_end)
  2733. #endif /* VI_MODE */
  2734.       {
  2735.         ding ();
  2736.         return;
  2737.       }
  2738.     else
  2739.       rl_point++;
  2740.     --count;
  2741.       }
  2742. }
  2743.  
  2744. /* Move backward COUNT characters. */
  2745. rl_backward (count)
  2746.      int count;
  2747. {
  2748.   if (count < 0)
  2749.     rl_forward (-count);
  2750.   else
  2751.     while (count)
  2752.       {
  2753.     if (!rl_point)
  2754.       {
  2755.         ding ();
  2756.         return;
  2757.       }
  2758.     else
  2759.       --rl_point;
  2760.     --count;
  2761.       }
  2762. }
  2763.  
  2764. /* Move to the beginning of the line. */
  2765. rl_beg_of_line ()
  2766. {
  2767.   rl_point = 0;
  2768. }
  2769.  
  2770. /* Move to the end of the line. */
  2771. rl_end_of_line ()
  2772. {
  2773.   rl_point = rl_end;
  2774. }
  2775.  
  2776. /* Move forward a word.  We do what Emacs does. */
  2777. rl_forward_word (count)
  2778.      int count;
  2779. {
  2780.   int c;
  2781.  
  2782.   if (count < 0)
  2783.     {
  2784.       rl_backward_word (-count);
  2785.       return;
  2786.     }
  2787.  
  2788.   while (count)
  2789.     {
  2790.       if (rl_point == rl_end)
  2791.     return;
  2792.  
  2793.       /* If we are not in a word, move forward until we are in one.
  2794.      Then, move forward until we hit a non-alphabetic character. */
  2795.       c = the_line[rl_point];
  2796.       if (!alphabetic (c))
  2797.     {
  2798.       while (++rl_point < rl_end)
  2799.         {
  2800.           c = the_line[rl_point];
  2801.           if (alphabetic (c)) break;
  2802.         }
  2803.     }
  2804.       if (rl_point == rl_end) return;
  2805.       while (++rl_point < rl_end)
  2806.     {
  2807.       c = the_line[rl_point];
  2808.       if (!alphabetic (c)) break;
  2809.     }
  2810.       --count;
  2811.     }
  2812. }
  2813.  
  2814. /* Move backward a word.  We do what Emacs does. */
  2815. rl_backward_word (count)
  2816.      int count;
  2817. {
  2818.   int c;
  2819.  
  2820.   if (count < 0)
  2821.     {
  2822.       rl_forward_word (-count);
  2823.       return;
  2824.     }
  2825.  
  2826.   while (count)
  2827.     {
  2828.       if (!rl_point)
  2829.     return;
  2830.  
  2831.       /* Like rl_forward_word (), except that we look at the characters
  2832.      just before point. */
  2833.  
  2834.       c = the_line[rl_point - 1];
  2835.       if (!alphabetic (c))
  2836.     {
  2837.       while (--rl_point)
  2838.         {
  2839.           c = the_line[rl_point - 1];
  2840.           if (alphabetic (c)) break;
  2841.         }
  2842.     }
  2843.  
  2844.       while (rl_point)
  2845.     {
  2846.       c = the_line[rl_point - 1];
  2847.       if (!alphabetic (c))
  2848.         break;
  2849.       else --rl_point;
  2850.     }
  2851.       --count;
  2852.     }
  2853. }
  2854.  
  2855. /* Clear the current line.  Numeric argument to C-l does this. */
  2856. rl_refresh_line ()
  2857. {
  2858.   int curr_line = last_c_pos / screenwidth;
  2859.   extern char *term_clreol;
  2860.  
  2861.   move_vert(curr_line);
  2862.   move_cursor_relative (0, the_line);   /* XXX is this right */
  2863.  
  2864.   if (term_clreol)
  2865.     tputs (term_clreol, 1, output_character_function);
  2866.  
  2867.   rl_forced_update_display ();
  2868.   rl_display_fixed = 1;
  2869. }
  2870.  
  2871. /* C-l typed to a line without quoting clears the screen, and then reprints
  2872.    the prompt and the current input line.  Given a numeric arg, redraw only
  2873.    the current line. */
  2874. rl_clear_screen ()
  2875. {
  2876.   extern char *term_clrpag;
  2877.  
  2878.   if (rl_explicit_arg)
  2879.     {
  2880.       rl_refresh_line ();
  2881.       return;
  2882.     }
  2883.  
  2884.   if (term_clrpag)
  2885.     tputs (term_clrpag, 1, output_character_function);
  2886.   else
  2887.     crlf ();
  2888.  
  2889.   rl_forced_update_display ();
  2890.   rl_display_fixed = 1;
  2891. }
  2892.  
  2893. rl_arrow_keys (count, c)
  2894.      int count, c;
  2895. {
  2896.   int ch;
  2897.  
  2898.   ch = rl_read_key ();
  2899.  
  2900.   switch (to_upper (ch))
  2901.     {
  2902.     case 'A':
  2903.       rl_get_previous_history (count);
  2904.       break;
  2905.  
  2906.     case 'B':
  2907.       rl_get_next_history (count);
  2908.       break;
  2909.  
  2910.     case 'C':
  2911.       rl_forward (count);
  2912.       break;
  2913.  
  2914.     case 'D':
  2915.       rl_backward (count);
  2916.       break;
  2917.  
  2918.     default:
  2919.       ding ();
  2920.     }
  2921. }
  2922.  
  2923.  
  2924. /* **************************************************************** */
  2925. /*                                    */
  2926. /*            Text commands                    */
  2927. /*                                    */
  2928. /* **************************************************************** */
  2929.  
  2930. /* Insert the character C at the current location, moving point forward. */
  2931. rl_insert (count, c)
  2932.      int count, c;
  2933. {
  2934.   register int i;
  2935.   char *string;
  2936.  
  2937.   if (count <= 0)
  2938.     return;
  2939.  
  2940.   /* If we can optimize, then do it.  But don't let people crash
  2941.      readline because of extra large arguments. */
  2942.   if (count > 1 && count < 1024)
  2943.     {
  2944.       string = (char *)alloca (1 + count);
  2945.  
  2946.       for (i = 0; i < count; i++)
  2947.     string[i] = c;
  2948.  
  2949.       string[i] = '\0';
  2950.       rl_insert_text (string);
  2951.       return;
  2952.     }
  2953.  
  2954.   if (count > 1024)
  2955.     {
  2956.       int decreaser;
  2957.  
  2958.       string = (char *)alloca (1024 + 1);
  2959.  
  2960.       for (i = 0; i < 1024; i++)
  2961.     string[i] = c;
  2962.  
  2963.       while (count)
  2964.     {
  2965.       decreaser = (count > 1024 ? 1024 : count);
  2966.       string[decreaser] = '\0';
  2967.       rl_insert_text (string);
  2968.       count -= decreaser;
  2969.     }
  2970.       return;
  2971.     }
  2972.  
  2973.   /* We are inserting a single character.
  2974.      If there is pending input, then make a string of all of the
  2975.      pending characters that are bound to rl_insert, and insert
  2976.      them all. */
  2977.   if (any_typein)
  2978.     {
  2979.       int key = 0, t;
  2980.  
  2981.       i = 0;
  2982.       string = (char *)alloca (ibuffer_len + 1);
  2983.       string[i++] = c;
  2984.  
  2985.       while ((t = rl_get_char (&key)) &&
  2986.          (keymap[key].type == ISFUNC &&
  2987.           keymap[key].function == rl_insert))
  2988.     string[i++] = key;
  2989.  
  2990.       if (t)
  2991.     rl_unget_char (key);
  2992.  
  2993.       string[i] = '\0';
  2994.       rl_insert_text (string);
  2995.       return;
  2996.     }
  2997.   else
  2998.     {
  2999.       /* Inserting a single character. */
  3000.       string = (char *)alloca (2);
  3001.  
  3002.       string[1] = '\0';
  3003.       string[0] = c;
  3004.       rl_insert_text (string);
  3005.     }
  3006. }
  3007.  
  3008. /* Insert the next typed character verbatim. */
  3009. rl_quoted_insert (count)
  3010.      int count;
  3011. {
  3012.   int c = rl_read_key ();
  3013.   rl_insert (count, c);
  3014. }
  3015.  
  3016. /* Insert a tab character. */
  3017. rl_tab_insert (count)
  3018.      int count;
  3019. {
  3020.   rl_insert (count, '\t');
  3021. }
  3022.  
  3023. /* What to do when a NEWLINE is pressed.  We accept the whole line.
  3024.    KEY is the key that invoked this command.  I guess it could have
  3025.    meaning in the future. */
  3026. rl_newline (count, key)
  3027.      int count, key;
  3028. {
  3029.  
  3030.   rl_done = 1;
  3031.  
  3032. #if defined (VI_MODE)
  3033.   {
  3034.     extern int vi_doing_insert;
  3035.     if (vi_doing_insert)
  3036.       {
  3037.     rl_end_undo_group ();
  3038.     vi_doing_insert = 0;
  3039.       }
  3040.   }
  3041. #endif /* VI_MODE */
  3042.  
  3043.   if (readline_echoing_p)
  3044.     {
  3045.       move_vert (vis_botlin);
  3046.       vis_botlin = 0;
  3047.       crlf ();
  3048.       fflush (out_stream);
  3049.       rl_display_fixed++;
  3050.     }
  3051. }
  3052.  
  3053. rl_clean_up_for_exit ()
  3054. {
  3055.   if (readline_echoing_p)
  3056.     {
  3057.       move_vert (vis_botlin);
  3058.       vis_botlin = 0;
  3059.       fflush (out_stream);
  3060.       rl_restart_output ();
  3061.     }
  3062. }
  3063.  
  3064. /* What to do for some uppercase characters, like meta characters,
  3065.    and some characters appearing in emacs_ctlx_keymap.  This function
  3066.    is just a stub, you bind keys to it and the code in rl_dispatch ()
  3067.    is special cased. */
  3068. rl_do_lowercase_version (ignore1, ignore2)
  3069.      int ignore1, ignore2;
  3070. {
  3071. }
  3072.  
  3073. /* Rubout the character behind point. */
  3074. rl_rubout (count)
  3075.      int count;
  3076. {
  3077.   if (count < 0)
  3078.     {
  3079.       rl_delete (-count);
  3080.       return;
  3081.     }
  3082.  
  3083.   if (!rl_point)
  3084.     {
  3085.       ding ();
  3086.       return;
  3087.     }
  3088.  
  3089.   if (count > 1)
  3090.     {
  3091.       int orig_point = rl_point;
  3092.       rl_backward (count);
  3093.       rl_kill_text (orig_point, rl_point);
  3094.     }
  3095.   else
  3096.     {
  3097.       int c = the_line[--rl_point];
  3098.       rl_delete_text (rl_point, rl_point + 1);
  3099.  
  3100.       if (rl_point == rl_end && alphabetic (c) && last_c_pos)
  3101.     {
  3102.       backspace (1);
  3103.       putc (' ', out_stream);
  3104.       backspace (1);
  3105.       last_c_pos--;
  3106.       visible_line[last_c_pos] = '\0';
  3107.       rl_display_fixed++;
  3108.     }
  3109.     }
  3110. }
  3111.  
  3112. /* Delete the character under the cursor.  Given a numeric argument,
  3113.    kill that many characters instead. */
  3114. rl_delete (count, invoking_key)
  3115.      int count, invoking_key;
  3116. {
  3117.   if (count < 0)
  3118.     {
  3119.       rl_rubout (-count);
  3120.       return;
  3121.     }
  3122.  
  3123.   if (rl_point == rl_end)
  3124.     {
  3125.       ding ();
  3126.       return;
  3127.     }
  3128.  
  3129.   if (count > 1)
  3130.     {
  3131.       int orig_point = rl_point;
  3132.       rl_forward (count);
  3133.       rl_kill_text (orig_point, rl_point);
  3134.       rl_point = orig_point;
  3135.     }
  3136.   else
  3137.     rl_delete_text (rl_point, rl_point + 1);
  3138. }
  3139.  
  3140.  
  3141. /* **************************************************************** */
  3142. /*                                    */
  3143. /*            Kill commands                    */
  3144. /*                                    */
  3145. /* **************************************************************** */
  3146.  
  3147. /* The next two functions mimic unix line editing behaviour, except they
  3148.    save the deleted text on the kill ring.  This is safer than not saving
  3149.    it, and since we have a ring, nobody should get screwed. */
  3150.  
  3151. /* This does what C-w does in Unix.  We can't prevent people from
  3152.    using behaviour that they expect. */
  3153. rl_unix_word_rubout ()
  3154. {
  3155.   if (!rl_point) ding ();
  3156.   else {
  3157.     int orig_point = rl_point;
  3158.     while (rl_point && whitespace (the_line[rl_point - 1]))
  3159.       rl_point--;
  3160.     while (rl_point && !whitespace (the_line[rl_point - 1]))
  3161.       rl_point--;
  3162.     rl_kill_text (rl_point, orig_point);
  3163.   }
  3164. }
  3165.  
  3166. /* Here is C-u doing what Unix does.  You don't *have* to use these
  3167.    key-bindings.  We have a choice of killing the entire line, or
  3168.    killing from where we are to the start of the line.  We choose the
  3169.    latter, because if you are a Unix weenie, then you haven't backspaced
  3170.    into the line at all, and if you aren't, then you know what you are
  3171.    doing. */
  3172. rl_unix_line_discard ()
  3173. {
  3174.   if (!rl_point) ding ();
  3175.   else {
  3176.     rl_kill_text (rl_point, 0);
  3177.     rl_point = 0;
  3178.   }
  3179. }
  3180.  
  3181.  
  3182.  
  3183. /* **************************************************************** */
  3184. /*                                    */
  3185. /*            Commands For Typos                */
  3186. /*                                    */
  3187. /* **************************************************************** */
  3188.  
  3189. /* Random and interesting things in here.  */
  3190.  
  3191. /* **************************************************************** */
  3192. /*                                    */
  3193. /*            Changing Case                    */
  3194. /*                                    */
  3195. /* **************************************************************** */
  3196.  
  3197. /* The three kinds of things that we know how to do. */
  3198. #define UpCase 1
  3199. #define DownCase 2
  3200. #define CapCase 3
  3201.  
  3202. /* Uppercase the word at point. */
  3203. rl_upcase_word (count)
  3204.      int count;
  3205. {
  3206.   rl_change_case (count, UpCase);
  3207. }
  3208.  
  3209. /* Lowercase the word at point. */
  3210. rl_downcase_word (count)
  3211.      int count;
  3212. {
  3213.   rl_change_case (count, DownCase);
  3214. }
  3215.  
  3216. /* Upcase the first letter, downcase the rest. */
  3217. rl_capitalize_word (count)
  3218.      int count;
  3219. {
  3220.   rl_change_case (count, CapCase);
  3221. }
  3222.  
  3223. /* The meaty function.
  3224.    Change the case of COUNT words, performing OP on them.
  3225.    OP is one of UpCase, DownCase, or CapCase.
  3226.    If a negative argument is given, leave point where it started,
  3227.    otherwise, leave it where it moves to. */
  3228. rl_change_case (count, op)
  3229.      int count, op;
  3230. {
  3231.   register int start = rl_point, end;
  3232.   int state = 0;
  3233.  
  3234.   rl_forward_word (count);
  3235.   end = rl_point;
  3236.  
  3237.   if (count < 0)
  3238.     {
  3239.       int temp = start;
  3240.       start = end;
  3241.       end = temp;
  3242.     }
  3243.  
  3244.   /* We are going to modify some text, so let's prepare to undo it. */
  3245.   rl_modifying (start, end);
  3246.  
  3247.   for (; start < end; start++)
  3248.     {
  3249.       switch (op)
  3250.     {
  3251.     case UpCase:
  3252.       the_line[start] = to_upper (the_line[start]);
  3253.       break;
  3254.  
  3255.     case DownCase:
  3256.       the_line[start] = to_lower (the_line[start]);
  3257.       break;
  3258.  
  3259.     case CapCase:
  3260.       if (state == 0)
  3261.         {
  3262.           the_line[start] = to_upper (the_line[start]);
  3263.           state = 1;
  3264.         }
  3265.       else
  3266.         {
  3267.           the_line[start] = to_lower (the_line[start]);
  3268.         }
  3269.       if (!pure_alphabetic (the_line[start]))
  3270.         state = 0;
  3271.       break;
  3272.  
  3273.     default:
  3274.       abort ();
  3275.     }
  3276.     }
  3277.   rl_point = end;
  3278. }
  3279.  
  3280. /* **************************************************************** */
  3281. /*                                    */
  3282. /*            Transposition                    */
  3283. /*                                    */
  3284. /* **************************************************************** */
  3285.  
  3286. /* Transpose the words at point. */
  3287. rl_transpose_words (count)
  3288.      int count;
  3289. {
  3290.   char *word1, *word2;
  3291.   int w1_beg, w1_end, w2_beg, w2_end;
  3292.   int orig_point = rl_point;
  3293.  
  3294.   if (!count) return;
  3295.  
  3296.   /* Find the two words. */
  3297.   rl_forward_word (count);
  3298.   w2_end = rl_point;
  3299.   rl_backward_word (1);
  3300.   w2_beg = rl_point;
  3301.   rl_backward_word (count);
  3302.   w1_beg = rl_point;
  3303.   rl_forward_word (1);
  3304.   w1_end = rl_point;
  3305.  
  3306.   /* Do some check to make sure that there really are two words. */
  3307.   if ((w1_beg == w2_beg) || (w2_beg < w1_end))
  3308.     {
  3309.       ding ();
  3310.       rl_point = orig_point;
  3311.       return;
  3312.     }
  3313.  
  3314.   /* Get the text of the words. */
  3315.   word1 = rl_copy (w1_beg, w1_end);
  3316.   word2 = rl_copy (w2_beg, w2_end);
  3317.  
  3318.   /* We are about to do many insertions and deletions.  Remember them
  3319.      as one operation. */
  3320.   rl_begin_undo_group ();
  3321.  
  3322.   /* Do the stuff at word2 first, so that we don't have to worry
  3323.      about word1 moving. */
  3324.   rl_point = w2_beg;
  3325.   rl_delete_text (w2_beg, w2_end);
  3326.   rl_insert_text (word1);
  3327.  
  3328.   rl_point = w1_beg;
  3329.   rl_delete_text (w1_beg, w1_end);
  3330.   rl_insert_text (word2);
  3331.  
  3332.   /* This is exactly correct since the text before this point has not
  3333.      changed in length. */
  3334.   rl_point = w2_end;
  3335.  
  3336.   /* I think that does it. */
  3337.   rl_end_undo_group ();
  3338.   free (word1); free (word2);
  3339. }
  3340.  
  3341. /* Transpose the characters at point.  If point is at the end of the line,
  3342.    then transpose the characters before point. */
  3343. rl_transpose_chars (count)
  3344.      int count;
  3345. {
  3346.   if (!count)
  3347.     return;
  3348.  
  3349.   if (!rl_point || rl_end < 2) {
  3350.     ding ();
  3351.     return;
  3352.   }
  3353.  
  3354.   while (count) {
  3355.     if (rl_point == rl_end) {
  3356.       int t = the_line[rl_point - 1];
  3357.       the_line[rl_point - 1] = the_line[rl_point - 2];
  3358.       the_line[rl_point - 2] = t;
  3359.     } else {
  3360.       int t = the_line[rl_point];
  3361.       the_line[rl_point] = the_line[rl_point - 1];
  3362.       the_line[rl_point - 1] = t;
  3363.       if (count < 0 && rl_point)
  3364.     rl_point--;
  3365.       else
  3366.     rl_point++;
  3367.     }
  3368.     if (count < 0)
  3369.       count++;
  3370.     else
  3371.       count--;
  3372.   }
  3373. }
  3374.  
  3375.  
  3376. /* **************************************************************** */
  3377. /*                                    */
  3378. /*            Bogus Flow Control                  */
  3379. /*                                    */
  3380. /* **************************************************************** */
  3381.  
  3382. rl_restart_output (count, key)
  3383.      int count, key;
  3384. {
  3385.   int fildes = fileno (rl_outstream);
  3386. #if defined (TIOCSTART)
  3387. #if defined (apollo)
  3388.   ioctl (&fildes, TIOCSTART, 0);
  3389. #else
  3390.   ioctl (fildes, TIOCSTART, 0);
  3391. #endif /* apollo */
  3392.  
  3393. #else
  3394. #  if defined (TERMIOS_TTY_DRIVER)
  3395.         tcflow (fildes, TCOON);
  3396. #  else
  3397. #    if defined (TCXONC)
  3398.         ioctl (fildes, TCXONC, TCOON);
  3399. #    endif /* TCXONC */
  3400. #  endif /* !TERMIOS_TTY_DRIVER */
  3401. #endif /* TIOCSTART */
  3402. }
  3403.  
  3404. rl_stop_output (count, key)
  3405.      int count, key;
  3406. {
  3407.   int fildes = fileno (rl_instream);
  3408.  
  3409. #if defined (TIOCSTOP)
  3410. # if defined (apollo)
  3411.   ioctl (&fildes, TIOCSTOP, 0);
  3412. # else
  3413.   ioctl (fildes, TIOCSTOP, 0);
  3414. # endif /* apollo */
  3415. #else
  3416. # if defined (TERMIOS_TTY_DRIVER)
  3417.   tcflow (fildes, TCOOFF);
  3418. # else
  3419. #   if defined (TCXONC)
  3420.   ioctl (fildes, TCXONC, TCOON);
  3421. #   endif /* TCXONC */
  3422. # endif /* !TERMIOS_TTY_DRIVER */
  3423. #endif /* TIOCSTOP */
  3424. }
  3425.  
  3426. /* **************************************************************** */
  3427. /*                                    */
  3428. /*    Completion matching, from readline's point of view.        */
  3429. /*                                    */
  3430. /* **************************************************************** */
  3431.  
  3432. /* Pointer to the generator function for completion_matches ().
  3433.    NULL means to use filename_entry_function (), the default filename
  3434.    completer. */
  3435. Function *rl_completion_entry_function = (Function *)NULL;
  3436.  
  3437. /* Pointer to alternative function to create matches.
  3438.    Function is called with TEXT, START, and END.
  3439.    START and END are indices in RL_LINE_BUFFER saying what the boundaries
  3440.    of TEXT are.
  3441.    If this function exists and returns NULL then call the value of
  3442.    rl_completion_entry_function to try to match, otherwise use the
  3443.    array of strings returned. */
  3444. Function *rl_attempted_completion_function = (Function *)NULL;
  3445.  
  3446. /* Complete the word at or before point.  You have supplied the function
  3447.    that does the initial simple matching selection algorithm (see
  3448.    completion_matches ()).  The default is to do filename completion. */
  3449. rl_complete (ignore, invoking_key)
  3450.      int ignore, invoking_key;
  3451. {
  3452.   if (rl_last_func == rl_complete)
  3453.     rl_complete_internal ('?');
  3454.   else
  3455.     rl_complete_internal (TAB);
  3456. }
  3457.  
  3458. /* List the possible completions.  See description of rl_complete (). */
  3459. rl_possible_completions ()
  3460. {
  3461.   rl_complete_internal ('?');
  3462. }
  3463.  
  3464. /* The user must press "y" or "n". Non-zero return means "y" pressed. */
  3465. get_y_or_n ()
  3466. {
  3467.   int c;
  3468.  loop:
  3469.   c = rl_read_key ();
  3470.   if (c == 'y' || c == 'Y') return (1);
  3471.   if (c == 'n' || c == 'N') return (0);
  3472.   if (c == ABORT_CHAR) rl_abort ();
  3473.   ding (); goto loop;
  3474. }
  3475.  
  3476. /* Up to this many items will be displayed in response to a
  3477.    possible-completions call.  After that, we ask the user if
  3478.    she is sure she wants to see them all. */
  3479. int rl_completion_query_items = 100;
  3480.  
  3481. /* The basic list of characters that signal a break between words for the
  3482.    completer routine.  The contents of this variable is what breaks words
  3483.    in the shell, i.e. " \t\n\"\\'`@$><=" */
  3484. char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(";
  3485.  
  3486. /* The list of characters that signal a break between words for
  3487.    rl_complete_internal.  The default list is the contents of
  3488.    rl_basic_word_break_characters.  */
  3489. char *rl_completer_word_break_characters = (char *)NULL;
  3490.  
  3491. /* List of characters that are word break characters, but should be left
  3492.    in TEXT when it is passed to the completion function.  The shell uses
  3493.    this to help determine what kind of completing to do. */
  3494. char *rl_special_prefixes = (char *)NULL;
  3495.  
  3496. /* If non-zero, then disallow duplicates in the matches. */
  3497. int rl_ignore_completion_duplicates = 1;
  3498.  
  3499. /* Non-zero means that the results of the matches are to be treated
  3500.    as filenames.  This is ALWAYS zero on entry, and can only be changed
  3501.    within a completion entry finder function. */
  3502. int rl_filename_completion_desired = 0;
  3503.  
  3504. /* This function, if defined, is called by the completer when real
  3505.    filename completion is done, after all the matching names have been
  3506.    generated. It is passed a (char**) known as matches in the code below.
  3507.    It consists of a NULL-terminated array of pointers to potential
  3508.    matching strings.  The 1st element (matches[0]) is the maximal
  3509.    substring that is common to all matches. This function can re-arrange
  3510.    the list of matches as required, but all elements of the array must be
  3511.    free()'d if they are deleted. The main intent of this function is
  3512.    to implement FIGNORE a la SunOS csh. */
  3513. Function *rl_ignore_some_completions_function = (Function *)NULL;
  3514.  
  3515. /* Complete the word at or before point.
  3516.    WHAT_TO_DO says what to do with the completion.
  3517.    `?' means list the possible completions.
  3518.    TAB means do standard completion.
  3519.    `*' means insert all of the possible completions. */
  3520. rl_complete_internal (what_to_do)
  3521.      int what_to_do;
  3522. {
  3523.   char *filename_completion_function ();
  3524.   char **completion_matches (), **matches;
  3525.   Function *our_func;
  3526.   int start, end, delimiter = 0;
  3527.   char *text;
  3528.  
  3529.   if (rl_completion_entry_function)
  3530.     our_func = rl_completion_entry_function;
  3531.   else
  3532.     our_func = (int (*)())filename_completion_function;
  3533.  
  3534.   /* Only the completion entry function can change this. */
  3535.   rl_filename_completion_desired = 0;
  3536.  
  3537.   /* We now look backwards for the start of a filename/variable word. */
  3538.   end = rl_point;
  3539.  
  3540.   if (rl_point)
  3541.     {
  3542.       while (--rl_point &&
  3543.          !rindex (rl_completer_word_break_characters, the_line[rl_point]));
  3544.  
  3545.       /* If we are at a word break, then advance past it. */
  3546.       if (rindex (rl_completer_word_break_characters, the_line[rl_point]))
  3547.     {
  3548.       /* If the character that caused the word break was a quoting
  3549.          character, then remember it as the delimiter. */
  3550.       if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1)
  3551.         delimiter = the_line[rl_point];
  3552.  
  3553.       /* If the character isn't needed to determine something special
  3554.          about what kind of completion to perform, then advance past it. */
  3555.  
  3556.       if (!rl_special_prefixes ||
  3557.           !rindex (rl_special_prefixes, the_line[rl_point]))
  3558.         rl_point++;
  3559.     }
  3560.     }
  3561.  
  3562.   start = rl_point;
  3563.   rl_point = end;
  3564.   text = rl_copy (start, end);
  3565.  
  3566.   /* If the user wants to TRY to complete, but then wants to give
  3567.      up and use the default completion function, they set the
  3568.      variable rl_attempted_completion_function. */
  3569.   if (rl_attempted_completion_function)
  3570.     {
  3571.       matches =
  3572.     (char **)(*rl_attempted_completion_function) (text, start, end);
  3573.  
  3574.       if (matches)
  3575.     {
  3576.       our_func = (Function *)NULL;
  3577.       goto after_usual_completion;
  3578.     }
  3579.     }
  3580.  
  3581.   matches = completion_matches (text, our_func);
  3582.  
  3583.  after_usual_completion:
  3584.   free (text);
  3585.  
  3586.   if (!matches)
  3587.     ding ();
  3588.   else
  3589.     {
  3590.       register int i;
  3591.  
  3592.     some_matches:
  3593.  
  3594.       /* It seems to me that in all the cases we handle we would like
  3595.      to ignore duplicate possiblilities.  Scan for the text to
  3596.      insert being identical to the other completions. */
  3597.       if (rl_ignore_completion_duplicates)
  3598.     {
  3599.       char *lowest_common;
  3600.       int j, newlen = 0;
  3601.  
  3602.       /* Sort the items. */
  3603.       /* It is safe to sort this array, because the lowest common
  3604.          denominator found in matches[0] will remain in place. */
  3605.       for (i = 0; matches[i]; i++);
  3606.       qsort (matches, i, sizeof (char *), compare_strings);
  3607.  
  3608.       /* Remember the lowest common denominator for it may be unique. */
  3609.       lowest_common = savestring (matches[0]);
  3610.  
  3611.       for (i = 0; matches[i + 1]; i++)
  3612.         {
  3613.           if (strcmp (matches[i], matches[i + 1]) == 0)
  3614.         {
  3615.           free (matches[i]);
  3616.           matches[i] = (char *)-1;
  3617.         }
  3618.           else
  3619.         newlen++;
  3620.         }
  3621.  
  3622.       /* We have marked all the dead slots with (char *)-1.
  3623.          Copy all the non-dead entries into a new array. */
  3624.       {
  3625.         char **temp_array =
  3626.           (char **)malloc ((3 + newlen) * sizeof (char *));
  3627.  
  3628.         for (i = 1, j = 1; matches[i]; i++)
  3629.           {
  3630.         if (matches[i] != (char *)-1)
  3631.           temp_array[j++] = matches[i];
  3632.           }
  3633.  
  3634.         temp_array[j] = (char *)NULL;
  3635.  
  3636.         if (matches[0] != (char *)-1)
  3637.           free (matches[0]);
  3638.  
  3639.         free (matches);
  3640.  
  3641.         matches = temp_array;
  3642.       }
  3643.  
  3644.       /* Place the lowest common denominator back in [0]. */
  3645.       matches[0] = lowest_common;
  3646.  
  3647.       /* If there is one string left, and it is identical to the
  3648.          lowest common denominator, then the LCD is the string to
  3649.          insert. */
  3650.       if (j == 2 && strcmp (matches[0], matches[1]) == 0)
  3651.         {
  3652.           free (matches[1]);
  3653.           matches[1] = (char *)NULL;
  3654.         }
  3655.     }
  3656.  
  3657.       switch (what_to_do)
  3658.     {
  3659.     case TAB:
  3660.       /* If we are matching filenames, then here is our chance to
  3661.          do clever processing by re-examining the list.  Call the
  3662.          ignore function with the array as a parameter.  It can
  3663.          munge the array, deleting matches as it desires */
  3664.       if (rl_ignore_some_completions_function &&
  3665.           our_func == (int (*)())filename_completion_function)
  3666.         (void)(*rl_ignore_some_completions_function)(matches);
  3667.  
  3668.       if (matches[0])
  3669.         {
  3670.           rl_delete_text (start, rl_point);
  3671.           rl_point = start;
  3672.           rl_insert_text (matches[0]);
  3673.         }
  3674.  
  3675.       /* If there are more matches, ring the bell to indicate.
  3676.          If this was the only match, and we are hacking files,
  3677.          check the file to see if it was a directory.  If so,
  3678.          add a '/' to the name.  If not, and we are at the end
  3679.          of the line, then add a space. */
  3680.       if (matches[1])
  3681.         {
  3682.           ding ();        /* There are other matches remaining. */
  3683.         }
  3684.       else
  3685.         {
  3686.           char temp_string[2];
  3687.  
  3688.           temp_string[0] = delimiter ? delimiter : ' ';
  3689.           temp_string[1] = '\0';
  3690.  
  3691.           if (rl_filename_completion_desired)
  3692.         {
  3693.           struct stat finfo;
  3694.           char *filename = tilde_expand (matches[0]);
  3695.  
  3696.           if ((stat (filename, &finfo) == 0) &&
  3697.               S_ISDIR (finfo.st_mode))
  3698.             {
  3699.               if (the_line[rl_point] != '/')
  3700.             rl_insert_text ("/");
  3701.             }
  3702.           else
  3703.             {
  3704.               if (rl_point == rl_end)
  3705.             rl_insert_text (temp_string);
  3706.             }
  3707.           free (filename);
  3708.         }
  3709.           else
  3710.         {
  3711.           if (rl_point == rl_end)
  3712.             rl_insert_text (temp_string);
  3713.         }
  3714.         }
  3715.       break;
  3716.  
  3717.     case '*':
  3718.       {
  3719.         int i = 1;
  3720.  
  3721.         rl_delete_text (start, rl_point);
  3722.         rl_point = start;
  3723.         rl_begin_undo_group ();
  3724.         if (matches[1])
  3725.           {
  3726.         while (matches[i])
  3727.           {
  3728.             rl_insert_text (matches[i++]);
  3729.             rl_insert_text (" ");
  3730.           }
  3731.           }
  3732.         else
  3733.           {
  3734.         rl_insert_text (matches[0]);
  3735.         rl_insert_text (" ");
  3736.           }
  3737.         rl_end_undo_group ();
  3738.       }
  3739.       break;
  3740.  
  3741.     case '?':
  3742.       {
  3743.         int len, count, limit, max = 0;
  3744.         int j, k, l;
  3745.  
  3746.         /* Handle simple case first.  What if there is only one answer? */
  3747.         if (!matches[1])
  3748.           {
  3749.         char *temp;
  3750.  
  3751.         if (rl_filename_completion_desired)
  3752.           temp = rindex (matches[0], '/');
  3753.         else
  3754.           temp = (char *)NULL;
  3755.  
  3756.         if (!temp)
  3757.           temp = matches[0];
  3758.         else
  3759.           temp++;
  3760.  
  3761.         crlf ();
  3762.         fprintf (out_stream, "%s", temp);
  3763.         crlf ();
  3764.         goto restart;
  3765.           }
  3766.  
  3767.         /* There is more than one answer.  Find out how many there are,
  3768.            and find out what the maximum printed length of a single entry
  3769.            is. */
  3770.         for (i = 1; matches[i]; i++)
  3771.           {
  3772.         char *temp = (char *)NULL;
  3773.  
  3774.         /* If we are hacking filenames, then only count the characters
  3775.            after the last slash in the pathname. */
  3776.         if (rl_filename_completion_desired)
  3777.           temp = rindex (matches[i], '/');
  3778.         else
  3779.           temp = (char *)NULL;
  3780.  
  3781.         if (!temp)
  3782.           temp = matches[i];
  3783.         else
  3784.           temp++;
  3785.  
  3786.         if (strlen (temp) > max)
  3787.           max = strlen (temp);
  3788.           }
  3789.  
  3790.         len = i;
  3791.  
  3792.         /* If there are many items, then ask the user if she
  3793.            really wants to see them all. */
  3794.         if (len >= rl_completion_query_items)
  3795.           {
  3796.         crlf ();
  3797.         fprintf (out_stream,
  3798.              "There are %d possibilities.  Do you really", len);
  3799.         crlf ();
  3800.         fprintf (out_stream, "wish to see them all? (y or n)");
  3801.         fflush (out_stream);
  3802.         if (!get_y_or_n ())
  3803.           {
  3804.             crlf ();
  3805.             goto restart;
  3806.           }
  3807.           }
  3808.         /* How many items of MAX length can we fit in the screen window? */
  3809.         max += 2;
  3810.         limit = screenwidth / max;
  3811.         if (limit != 1 && (limit * max == screenwidth))
  3812.           limit--;
  3813.  
  3814.         /* Avoid a possible floating exception.  If max > screenwidth,
  3815.            limit will be 0 and a divide-by-zero fault will result. */
  3816.         if (limit == 0)
  3817.           limit = 1;
  3818.  
  3819.         /* How many iterations of the printing loop? */
  3820.         count = (len + (limit - 1)) / limit;
  3821.  
  3822.         /* Watch out for special case.  If LEN is less than LIMIT, then
  3823.            just do the inner printing loop. */
  3824.         if (len < limit) count = 1;
  3825.  
  3826.         /* Sort the items if they are not already sorted. */
  3827.         if (!rl_ignore_completion_duplicates)
  3828.           qsort (matches, len, sizeof (char *), compare_strings);
  3829.  
  3830.         /* Print the sorted items, up-and-down alphabetically, like
  3831.            ls might. */
  3832.         crlf ();
  3833.  
  3834.         for (i = 1; i < count + 1; i++)
  3835.           {
  3836.         for (j = 0, l = i; j < limit; j++)
  3837.           {
  3838.             if (l > len || !matches[l])
  3839.               {
  3840.             break;
  3841.               }
  3842.             else
  3843.               {
  3844.             char *temp = (char *)NULL;
  3845.  
  3846.             if (rl_filename_completion_desired)
  3847.               temp = rindex (matches[l], '/');
  3848.             else
  3849.               temp = (char *)NULL;
  3850.  
  3851.             if (!temp)
  3852.               temp = matches[l];
  3853.             else
  3854.               temp++;
  3855.  
  3856.             fprintf (out_stream, "%s", temp);
  3857.             for (k = 0; k < max - strlen (temp); k++)
  3858.               putc (' ', out_stream);
  3859.               }
  3860.             l += count;
  3861.           }
  3862.         crlf ();
  3863.           }
  3864.       restart:
  3865.  
  3866.         rl_on_new_line ();
  3867.       }
  3868.       break;
  3869.  
  3870.     default:
  3871.       abort ();
  3872.     }
  3873.  
  3874.       for (i = 0; matches[i]; i++)
  3875.     free (matches[i]);
  3876.       free (matches);
  3877.     }
  3878. }
  3879.  
  3880. /* Stupid comparison routine for qsort () ing strings. */
  3881. static int
  3882. compare_strings (s1, s2)
  3883.   char **s1, **s2;
  3884. {
  3885.   return (strcmp (*s1, *s2));
  3886. }
  3887.  
  3888. /* A completion function for usernames.
  3889.    TEXT contains a partial username preceded by a random
  3890.    character (usually `~').  */
  3891. char *
  3892. username_completion_function (text, state)
  3893.      int state;
  3894.      char *text;
  3895. {
  3896.   static char *username = (char *)NULL;
  3897.   static struct passwd *entry;
  3898.   static int namelen, first_char, first_char_loc;
  3899.  
  3900.   if (!state)
  3901.     {
  3902.       if (username)
  3903.     free (username);
  3904.  
  3905.       first_char = *text;
  3906.  
  3907.       if (first_char == '~')
  3908.     first_char_loc = 1;
  3909.       else
  3910.     first_char_loc = 0;
  3911.  
  3912.       username = savestring (&text[first_char_loc]);
  3913.       namelen = strlen (username);
  3914.       setpwent ();
  3915.     }
  3916.  
  3917.   while (entry = getpwent ())
  3918.     {
  3919.       if (strncmp (username, entry->pw_name, namelen) == 0)
  3920.     break;
  3921.     }
  3922.  
  3923.   if (!entry)
  3924.     {
  3925.       endpwent ();
  3926.       return ((char *)NULL);
  3927.     }
  3928.   else
  3929.     {
  3930.       char *value = (char *)xmalloc (2 + strlen (entry->pw_name));
  3931.  
  3932.       *value = *text;
  3933.  
  3934.       strcpy (value + first_char_loc, entry->pw_name);
  3935.  
  3936.       if (first_char == '~')
  3937.     rl_filename_completion_desired = 1;
  3938.  
  3939.       return (value);
  3940.     }
  3941. }
  3942.  
  3943. /* **************************************************************** */
  3944. /*                                    */
  3945. /*            Undo, and Undoing                */
  3946. /*                                    */
  3947. /* **************************************************************** */
  3948.  
  3949. /* Non-zero tells rl_delete_text and rl_insert_text to not add to
  3950.    the undo list. */
  3951. int doing_an_undo = 0;
  3952.  
  3953. /* The current undo list for THE_LINE. */
  3954. UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
  3955.  
  3956. /* Remember how to undo something.  Concatenate some undos if that
  3957.    seems right. */
  3958. rl_add_undo (what, start, end, text)
  3959.      enum undo_code what;
  3960.      int start, end;
  3961.      char *text;
  3962. {
  3963.   UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
  3964.   temp->what = what;
  3965.   temp->start = start;
  3966.   temp->end = end;
  3967.   temp->text = text;
  3968.   temp->next = rl_undo_list;
  3969.   rl_undo_list = temp;
  3970. }
  3971.  
  3972. /* Free the existing undo list. */
  3973. free_undo_list ()
  3974. {
  3975.   while (rl_undo_list) {
  3976.     UNDO_LIST *release = rl_undo_list;
  3977.     rl_undo_list = rl_undo_list->next;
  3978.  
  3979.     if (release->what == UNDO_DELETE)
  3980.       free (release->text);
  3981.  
  3982.     free (release);
  3983.   }
  3984. }
  3985.  
  3986. /* Undo the next thing in the list.  Return 0 if there
  3987.    is nothing to undo, or non-zero if there was. */
  3988. int
  3989. rl_do_undo ()
  3990. {
  3991.   UNDO_LIST *release;
  3992.   int waiting_for_begin = 0;
  3993.  
  3994. undo_thing:
  3995.   if (!rl_undo_list)
  3996.     return (0);
  3997.  
  3998.   doing_an_undo = 1;
  3999.  
  4000.   switch (rl_undo_list->what) {
  4001.  
  4002.     /* Undoing deletes means inserting some text. */
  4003.   case UNDO_DELETE:
  4004.     rl_point = rl_undo_list->start;
  4005.     rl_insert_text (rl_undo_list->text);
  4006.     free (rl_undo_list->text);
  4007.     break;
  4008.  
  4009.     /* Undoing inserts means deleting some text. */
  4010.   case UNDO_INSERT:
  4011.     rl_delete_text (rl_undo_list->start, rl_undo_list->end);
  4012.     rl_point = rl_undo_list->start;
  4013.     break;
  4014.  
  4015.     /* Undoing an END means undoing everything 'til we get to
  4016.        a BEGIN. */
  4017.   case UNDO_END:
  4018.     waiting_for_begin++;
  4019.     break;
  4020.  
  4021.     /* Undoing a BEGIN means that we are done with this group. */
  4022.   case UNDO_BEGIN:
  4023.     if (waiting_for_begin)
  4024.       waiting_for_begin--;
  4025.     else
  4026.       abort ();
  4027.     break;
  4028.   }
  4029.  
  4030.   doing_an_undo = 0;
  4031.  
  4032.   release = rl_undo_list;
  4033.   rl_undo_list = rl_undo_list->next;
  4034.   free (release);
  4035.  
  4036.   if (waiting_for_begin)
  4037.     goto undo_thing;
  4038.  
  4039.   return (1);
  4040. }
  4041.  
  4042. /* Begin a group.  Subsequent undos are undone as an atomic operation. */
  4043. rl_begin_undo_group ()
  4044. {
  4045.   rl_add_undo (UNDO_BEGIN, 0, 0, 0);
  4046. }
  4047.  
  4048. /* End an undo group started with rl_begin_undo_group (). */
  4049. rl_end_undo_group ()
  4050. {
  4051.   rl_add_undo (UNDO_END, 0, 0, 0);
  4052. }
  4053.  
  4054. /* Save an undo entry for the text from START to END. */
  4055. rl_modifying (start, end)
  4056.      int start, end;
  4057. {
  4058.   if (start > end)
  4059.     {
  4060.       int t = start;
  4061.       start = end;
  4062.       end = t;
  4063.     }
  4064.  
  4065.   if (start != end)
  4066.     {
  4067.       char *temp = rl_copy (start, end);
  4068.       rl_begin_undo_group ();
  4069.       rl_add_undo (UNDO_DELETE, start, end, temp);
  4070.       rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
  4071.       rl_end_undo_group ();
  4072.     }
  4073. }
  4074.  
  4075. /* Revert the current line to its previous state. */
  4076. rl_revert_line ()
  4077. {
  4078.   if (!rl_undo_list) ding ();
  4079.   else {
  4080.     while (rl_undo_list)
  4081.       rl_do_undo ();
  4082.   }
  4083. }
  4084.  
  4085. /* Do some undoing of things that were done. */
  4086. rl_undo_command (count)
  4087. {
  4088.   if (count < 0) return;    /* Nothing to do. */
  4089.  
  4090.   while (count)
  4091.     {
  4092.       if (rl_do_undo ())
  4093.     {
  4094.       count--;
  4095.     }
  4096.       else
  4097.     {
  4098.       ding ();
  4099.       break;
  4100.     }
  4101.     }
  4102. }
  4103.  
  4104. /* **************************************************************** */
  4105. /*                                    */
  4106. /*            History Utilities                */
  4107. /*                                    */
  4108. /* **************************************************************** */
  4109.  
  4110. /* We already have a history library, and that is what we use to control
  4111.    the history features of readline.  However, this is our local interface
  4112.    to the history mechanism. */
  4113.  
  4114. /* While we are editing the history, this is the saved
  4115.    version of the original line. */
  4116. HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
  4117.  
  4118. /* Set the history pointer back to the last entry in the history. */
  4119. start_using_history ()
  4120. {
  4121.   using_history ();
  4122.   if (saved_line_for_history)
  4123.     free_history_entry (saved_line_for_history);
  4124.  
  4125.   saved_line_for_history = (HIST_ENTRY *)NULL;
  4126. }
  4127.  
  4128. /* Free the contents (and containing structure) of a HIST_ENTRY. */
  4129. free_history_entry (entry)
  4130.      HIST_ENTRY *entry;
  4131. {
  4132.   if (!entry) return;
  4133.   if (entry->line)
  4134.     free (entry->line);
  4135.   free (entry);
  4136. }
  4137.  
  4138. /* Perhaps put back the current line if it has changed. */
  4139. maybe_replace_line ()
  4140. {
  4141.   HIST_ENTRY *temp = current_history ();
  4142.  
  4143.   /* If the current line has changed, save the changes. */
  4144.   if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
  4145.     {
  4146.       temp = replace_history_entry (where_history (), the_line, rl_undo_list);
  4147.       free (temp->line);
  4148.       free (temp);
  4149.     }
  4150. }
  4151.  
  4152. /* Put back the saved_line_for_history if there is one. */
  4153. maybe_unsave_line ()
  4154. {
  4155.   if (saved_line_for_history)
  4156.     {
  4157.       int line_len;
  4158.  
  4159.       line_len = strlen (saved_line_for_history->line);
  4160.  
  4161.       if (line_len >= rl_line_buffer_len)
  4162.     rl_extend_line_buffer (line_len);
  4163.  
  4164.       strcpy (the_line, saved_line_for_history->line);
  4165.       rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
  4166.       free_history_entry (saved_line_for_history);
  4167.       saved_line_for_history = (HIST_ENTRY *)NULL;
  4168.       rl_end = rl_point = strlen (the_line);
  4169.     }
  4170.   else
  4171.     ding ();
  4172. }
  4173.  
  4174. /* Save the current line in saved_line_for_history. */
  4175. maybe_save_line ()
  4176. {
  4177.   if (!saved_line_for_history)
  4178.     {
  4179.       saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  4180.       saved_line_for_history->line = savestring (the_line);
  4181.       saved_line_for_history->data = (char *)rl_undo_list;
  4182.     }
  4183. }
  4184.  
  4185. /* **************************************************************** */
  4186. /*                                    */
  4187. /*            History Commands                */
  4188. /*                                    */
  4189. /* **************************************************************** */
  4190.  
  4191. /* Meta-< goes to the start of the history. */
  4192. rl_beginning_of_history ()
  4193. {
  4194.   rl_get_previous_history (1 + where_history ());
  4195. }
  4196.  
  4197. /* Meta-> goes to the end of the history.  (The current line). */
  4198. rl_end_of_history ()
  4199. {
  4200.   maybe_replace_line ();
  4201.   using_history ();
  4202.   maybe_unsave_line ();
  4203. }
  4204.  
  4205. /* Move down to the next history line. */
  4206. rl_get_next_history (count)
  4207.      int count;
  4208. {
  4209.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  4210.  
  4211.   if (count < 0)
  4212.     {
  4213.       rl_get_previous_history (-count);
  4214.       return;
  4215.     }
  4216.  
  4217.   if (!count)
  4218.     return;
  4219.  
  4220.   maybe_replace_line ();
  4221.  
  4222.   while (count)
  4223.     {
  4224.       temp = next_history ();
  4225.       if (!temp)
  4226.     break;
  4227.       --count;
  4228.     }
  4229.  
  4230.   if (!temp)
  4231.     maybe_unsave_line ();
  4232.   else
  4233.     {
  4234.       int line_len;
  4235.  
  4236.       line_len = strlen (temp->line);
  4237.  
  4238.       if (line_len >= rl_line_buffer_len)
  4239.     rl_extend_line_buffer (line_len);
  4240.  
  4241.       strcpy (the_line, temp->line);
  4242.       rl_undo_list = (UNDO_LIST *)temp->data;
  4243.       rl_end = rl_point = strlen (the_line);
  4244. #if defined (VI_MODE)
  4245.       if (rl_editing_mode == vi_mode)
  4246.     rl_point = 0;
  4247. #endif /* VI_MODE */
  4248.     }
  4249. }
  4250.  
  4251. /* Get the previous item out of our interactive history, making it the current
  4252.    line.  If there is no previous history, just ding. */
  4253. rl_get_previous_history (count)
  4254.      int count;
  4255. {
  4256.   HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
  4257.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  4258.  
  4259.   if (count < 0)
  4260.     {
  4261.       rl_get_next_history (-count);
  4262.       return;
  4263.     }
  4264.  
  4265.   if (!count)
  4266.     return;
  4267.  
  4268.   /* If we don't have a line saved, then save this one. */
  4269.   maybe_save_line ();
  4270.  
  4271.   /* If the current line has changed, save the changes. */
  4272.   maybe_replace_line ();
  4273.  
  4274.   while (count)
  4275.     {
  4276.       temp = previous_history ();
  4277.       if (!temp)
  4278.     break;
  4279.       else
  4280.     old_temp = temp;
  4281.       --count;
  4282.     }
  4283.  
  4284.   /* If there was a large argument, and we moved back to the start of the
  4285.      history, that is not an error.  So use the last value found. */
  4286.   if (!temp && old_temp)
  4287.     temp = old_temp;
  4288.  
  4289.   if (!temp)
  4290.     ding ();
  4291.   else
  4292.     {
  4293.       int line_len;
  4294.  
  4295.       line_len = strlen (temp->line);
  4296.  
  4297.       if (line_len >= rl_line_buffer_len)
  4298.     rl_extend_line_buffer (line_len);
  4299.  
  4300.       strcpy (the_line, temp->line);
  4301.       rl_undo_list = (UNDO_LIST *)temp->data;
  4302.       rl_end = rl_point = line_len;
  4303.  
  4304. #if defined (VI_MODE)
  4305.       if (rl_editing_mode == vi_mode)
  4306.     rl_point = 0;
  4307. #endif /* VI_MODE */
  4308.     }
  4309. }
  4310.  
  4311.  
  4312. /* **************************************************************** */
  4313. /*                                    */
  4314. /*            I-Search and Searching                */
  4315. /*                                    */
  4316. /* **************************************************************** */
  4317.  
  4318. /* Search backwards through the history looking for a string which is typed
  4319.    interactively.  Start with the current line. */
  4320. rl_reverse_search_history (sign, key)
  4321.      int sign;
  4322.      int key;
  4323. {
  4324.   rl_search_history (-sign, key);
  4325. }
  4326.  
  4327. /* Search forwards through the history looking for a string which is typed
  4328.    interactively.  Start with the current line. */
  4329. rl_forward_search_history (sign, key)
  4330.      int sign;
  4331.      int key;
  4332. {
  4333.   rl_search_history (sign, key);
  4334. }
  4335.  
  4336. /* Display the current state of the search in the echo-area.
  4337.    SEARCH_STRING contains the string that is being searched for,
  4338.    DIRECTION is zero for forward, or 1 for reverse,
  4339.    WHERE is the history list number of the current line.  If it is
  4340.    -1, then this line is the starting one. */
  4341. rl_display_search (search_string, reverse_p, where)
  4342.      char *search_string;
  4343.      int reverse_p, where;
  4344. {
  4345.   char *message = (char *)NULL;
  4346.  
  4347.   message =
  4348.     (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30);
  4349.  
  4350.   *message = '\0';
  4351.  
  4352. #if defined (NOTDEF)
  4353.   if (where != -1)
  4354.     sprintf (message, "[%d]", where + history_base);
  4355. #endif /* NOTDEF */
  4356.  
  4357.   strcat (message, "(");
  4358.  
  4359.   if (reverse_p)
  4360.     strcat (message, "reverse-");
  4361.  
  4362.   strcat (message, "i-search)`");
  4363.  
  4364.   if (search_string)
  4365.     strcat (message, search_string);
  4366.  
  4367.   strcat (message, "': ");
  4368.   rl_message (message, 0, 0);
  4369.   rl_redisplay ();
  4370. }
  4371.  
  4372. /* Search through the history looking for an interactively typed string.
  4373.    This is analogous to i-search.  We start the search in the current line.
  4374.    DIRECTION is which direction to search; >= 0 means forward, < 0 means
  4375.    backwards. */
  4376. rl_search_history (direction, invoking_key)
  4377.      int direction;
  4378.      int invoking_key;
  4379. {
  4380.   /* The string that the user types in to search for. */
  4381.   char *search_string = (char *)alloca (128);
  4382.  
  4383.   /* The current length of SEARCH_STRING. */
  4384.   int search_string_index;
  4385.  
  4386.   /* The list of lines to search through. */
  4387.   char **lines;
  4388.  
  4389.   /* The length of LINES. */
  4390.   int hlen;
  4391.  
  4392.   /* Where we get LINES from. */
  4393.   HIST_ENTRY **hlist = history_list ();
  4394.  
  4395.   register int i = 0;
  4396.   int orig_point = rl_point;
  4397.   int orig_line = where_history ();
  4398.   int last_found_line = orig_line;
  4399.   int c, done = 0;
  4400.  
  4401.   /* The line currently being searched. */
  4402.   char *sline;
  4403.  
  4404.   /* Offset in that line. */
  4405.   int index;
  4406.  
  4407.   /* Non-zero if we are doing a reverse search. */
  4408.   int reverse = (direction < 0);
  4409.  
  4410.   /* Create an arrary of pointers to the lines that we want to search. */
  4411.   maybe_replace_line ();
  4412.   if (hlist)
  4413.     for (i = 0; hlist[i]; i++);
  4414.  
  4415.   /* Allocate space for this many lines, +1 for the current input line,
  4416.      and remember those lines. */
  4417.   lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
  4418.   for (i = 0; i < hlen; i++)
  4419.     lines[i] = hlist[i]->line;
  4420.  
  4421.   if (saved_line_for_history)
  4422.     lines[i] = saved_line_for_history->line;
  4423.   else
  4424.     /* So I have to type it in this way instead. */
  4425.     {
  4426.       char *alloced_line;
  4427.  
  4428.       /* Keep that mips alloca happy. */
  4429.       alloced_line = (char *)alloca (1 + strlen (the_line));
  4430.       lines[i] = alloced_line;
  4431.       strcpy (lines[i], &the_line[0]);
  4432.     }
  4433.  
  4434.   hlen++;
  4435.  
  4436.   /* The line where we start the search. */
  4437.   i = orig_line;
  4438.  
  4439.   /* Initialize search parameters. */
  4440.   *search_string = '\0';
  4441.   search_string_index = 0;
  4442.  
  4443.   /* Normalize DIRECTION into 1 or -1. */
  4444.   if (direction >= 0)
  4445.     direction = 1;
  4446.   else
  4447.     direction = -1;
  4448.  
  4449.   rl_display_search (search_string, reverse, -1);
  4450.  
  4451.   sline = the_line;
  4452.   index = rl_point;
  4453.  
  4454.   while (!done)
  4455.     {
  4456.       c = rl_read_key ();
  4457.  
  4458.       /* Hack C to Do What I Mean. */
  4459.       {
  4460.     Function *f = (Function *)NULL;
  4461.  
  4462.     if (keymap[c].type == ISFUNC)
  4463.       {
  4464.         f = keymap[c].function;
  4465.  
  4466.         if (f == rl_reverse_search_history)
  4467.           c = reverse ? -1 : -2;
  4468.         else if (f == rl_forward_search_history)
  4469.           c =  !reverse ? -1 : -2;
  4470.       }
  4471.       }
  4472.  
  4473.       switch (c)
  4474.     {
  4475.     case ESC:
  4476.       done = 1;
  4477.       continue;
  4478.  
  4479.       /* case invoking_key: */
  4480.     case -1:
  4481.       goto search_again;
  4482.  
  4483.       /* switch directions */
  4484.     case -2:
  4485.       direction = -direction;
  4486.       reverse = (direction < 0);
  4487.  
  4488.       goto do_search;
  4489.  
  4490.     case CTRL ('G'):
  4491.       strcpy (the_line, lines[orig_line]);
  4492.       rl_point = orig_point;
  4493.       rl_end = strlen (the_line);
  4494.       rl_clear_message ();
  4495.       return;
  4496.  
  4497.     default:
  4498.       if (c < 32 || c > 126)
  4499.         {
  4500.           rl_execute_next (c);
  4501.           done = 1;
  4502.           continue;
  4503.         }
  4504.       else
  4505.         {
  4506.           search_string[search_string_index++] = c;
  4507.           search_string[search_string_index] = '\0';
  4508.           goto do_search;
  4509.  
  4510.         search_again:
  4511.  
  4512.           if (!search_string_index)
  4513.         continue;
  4514.           else
  4515.         {
  4516.           if (reverse)
  4517.             --index;
  4518.           else
  4519.             if (index != strlen (sline))
  4520.               ++index;
  4521.             else
  4522.               ding ();
  4523.         }
  4524.         do_search:
  4525.  
  4526.           while (1)
  4527.         {
  4528.           if (reverse)
  4529.             {
  4530.               while (index >= 0)
  4531.             if (strncmp
  4532.                 (search_string, sline + index, search_string_index)
  4533.                 == 0)
  4534.               goto string_found;
  4535.             else
  4536.               index--;
  4537.             }
  4538.           else
  4539.             {
  4540.               register int limit =
  4541.             (strlen (sline) - search_string_index) + 1;
  4542.  
  4543.               while (index < limit)
  4544.             {
  4545.               if (strncmp (search_string,
  4546.                        sline + index,
  4547.                        search_string_index) == 0)
  4548.                 goto string_found;
  4549.               index++;
  4550.             }
  4551.             }
  4552.  
  4553.         next_line:
  4554.           i += direction;
  4555.  
  4556.           /* At limit for direction? */
  4557.           if ((reverse && i < 0) ||
  4558.               (!reverse && i == hlen))
  4559.             goto search_failed;
  4560.  
  4561.           sline = lines[i];
  4562.           if (reverse)
  4563.             index = strlen (sline);
  4564.           else
  4565.             index = 0;
  4566.  
  4567.           /* If the search string is longer than the current
  4568.              line, no match. */
  4569.           if (search_string_index > strlen (sline))
  4570.             goto next_line;
  4571.  
  4572.           /* Start actually searching. */
  4573.           if (reverse)
  4574.             index -= search_string_index;
  4575.         }
  4576.  
  4577.         search_failed:
  4578.           /* We cannot find the search string.  Ding the bell. */
  4579.           ding ();
  4580.           i = last_found_line;
  4581.           break;
  4582.  
  4583.         string_found:
  4584.           /* We have found the search string.  Just display it.  But don't
  4585.          actually move there in the history list until the user accepts
  4586.          the location. */
  4587.           {
  4588.         int line_len;
  4589.  
  4590.         line_len = strlen (lines[i]);
  4591.  
  4592.         if (line_len >= rl_line_buffer_len)
  4593.           rl_extend_line_buffer (line_len);
  4594.  
  4595.         strcpy (the_line, lines[i]);
  4596.         rl_point = index;
  4597.         rl_end = line_len;
  4598.         last_found_line = i;
  4599.         rl_display_search
  4600.           (search_string, reverse, (i == orig_line) ? -1 : i);
  4601.           }
  4602.         }
  4603.     }
  4604.       continue;
  4605.     }
  4606.  
  4607.   /* The searching is over.  The user may have found the string that she
  4608.      was looking for, or else she may have exited a failing search.  If
  4609.      INDEX is -1, then that shows that the string searched for was not
  4610.      found.  We use this to determine where to place rl_point. */
  4611.   {
  4612.     int now = last_found_line;
  4613.  
  4614.     /* First put back the original state. */
  4615.     strcpy (the_line, lines[orig_line]);
  4616.  
  4617.     if (now < orig_line)
  4618.       rl_get_previous_history (orig_line - now);
  4619.     else
  4620.       rl_get_next_history (now - orig_line);
  4621.  
  4622.     /* If the index of the "matched" string is less than zero, then the
  4623.        final search string was never matched, so put point somewhere
  4624.        reasonable. */
  4625.     if (index < 0)
  4626.       index = strlen (the_line);
  4627.  
  4628.     rl_point = index;
  4629.     rl_clear_message ();
  4630.   }
  4631. }
  4632.  
  4633. /* Make C be the next command to be executed. */
  4634. rl_execute_next (c)
  4635.      int c;
  4636. {
  4637.   rl_pending_input = c;
  4638. }
  4639.  
  4640. /* **************************************************************** */
  4641. /*                                    */
  4642. /*            Killing Mechanism                */
  4643. /*                                    */
  4644. /* **************************************************************** */
  4645.  
  4646. /* What we assume for a max number of kills. */
  4647. #define DEFAULT_MAX_KILLS 10
  4648.  
  4649. /* The real variable to look at to find out when to flush kills. */
  4650. int rl_max_kills = DEFAULT_MAX_KILLS;
  4651.  
  4652. /* Where to store killed text. */
  4653. char **rl_kill_ring = (char **)NULL;
  4654.  
  4655. /* Where we are in the kill ring. */
  4656. int rl_kill_index = 0;
  4657.  
  4658. /* How many slots we have in the kill ring. */
  4659. int rl_kill_ring_length = 0;
  4660.  
  4661. /* How to say that you only want to save a certain amount
  4662.    of kill material. */
  4663. rl_set_retained_kills (num)
  4664.      int num;
  4665. {}
  4666.  
  4667. /* The way to kill something.  This appends or prepends to the last
  4668.    kill, if the last command was a kill command.  if FROM is less
  4669.    than TO, then the text is appended, otherwise prepended.  If the
  4670.    last command was not a kill command, then a new slot is made for
  4671.    this kill. */
  4672. rl_kill_text (from, to)
  4673.      int from, to;
  4674. {
  4675.   int slot;
  4676.   char *text = rl_copy (from, to);
  4677.  
  4678.   /* Is there anything to kill? */
  4679.   if (from == to)
  4680.     {
  4681.       free (text);
  4682.       last_command_was_kill++;
  4683.       return;
  4684.     }
  4685.  
  4686.   /* Delete the copied text from the line. */
  4687.   rl_delete_text (from, to);
  4688.  
  4689.   /* First, find the slot to work with. */
  4690.   if (!last_command_was_kill)
  4691.     {
  4692.       /* Get a new slot.  */
  4693.       if (!rl_kill_ring)
  4694.     {
  4695.       /* If we don't have any defined, then make one. */
  4696.       rl_kill_ring = (char **)
  4697.         xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
  4698.       slot = 1;
  4699.     }
  4700.       else
  4701.     {
  4702.       /* We have to add a new slot on the end, unless we have
  4703.          exceeded the max limit for remembering kills. */
  4704.       slot = rl_kill_ring_length;
  4705.       if (slot == rl_max_kills)
  4706.         {
  4707.           register int i;
  4708.           free (rl_kill_ring[0]);
  4709.           for (i = 0; i < slot; i++)
  4710.         rl_kill_ring[i] = rl_kill_ring[i + 1];
  4711.         }
  4712.       else
  4713.         {
  4714.           rl_kill_ring =
  4715.         (char **)
  4716.           xrealloc (rl_kill_ring,
  4717.                 ((slot = (rl_kill_ring_length += 1)) + 1)
  4718.                 * sizeof (char *));
  4719.         }
  4720.     }
  4721.       slot--;
  4722.     }
  4723.   else
  4724.     {
  4725.       slot = rl_kill_ring_length - 1;
  4726.     }
  4727.  
  4728.   /* If the last command was a kill, prepend or append. */
  4729.   if (last_command_was_kill && rl_editing_mode != vi_mode)
  4730.     {
  4731.       char *old = rl_kill_ring[slot];
  4732.       char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
  4733.  
  4734.       if (from < to)
  4735.     {
  4736.       strcpy (new, old);
  4737.       strcat (new, text);
  4738.     }
  4739.       else
  4740.     {
  4741.       strcpy (new, text);
  4742.       strcat (new, old);
  4743.     }
  4744.       free (old);
  4745.       free (text);
  4746.       rl_kill_ring[slot] = new;
  4747.     }
  4748.   else
  4749.     {
  4750.       rl_kill_ring[slot] = text;
  4751.     }
  4752.   rl_kill_index = slot;
  4753.   last_command_was_kill++;
  4754. }
  4755.  
  4756. /* Now REMEMBER!  In order to do prepending or appending correctly, kill
  4757.    commands always make rl_point's original position be the FROM argument,
  4758.    and rl_point's extent be the TO argument. */
  4759.  
  4760. /* **************************************************************** */
  4761. /*                                    */
  4762. /*            Killing Commands                */
  4763. /*                                    */
  4764. /* **************************************************************** */
  4765.  
  4766. /* Delete the word at point, saving the text in the kill ring. */
  4767. rl_kill_word (count)
  4768.      int count;
  4769. {
  4770.   int orig_point = rl_point;
  4771.  
  4772.   if (count < 0)
  4773.     rl_backward_kill_word (-count);
  4774.   else
  4775.     {
  4776.       rl_forward_word (count);
  4777.  
  4778.       if (rl_point != orig_point)
  4779.     rl_kill_text (orig_point, rl_point);
  4780.  
  4781.       rl_point = orig_point;
  4782.     }
  4783. }
  4784.  
  4785. /* Rubout the word before point, placing it on the kill ring. */
  4786. rl_backward_kill_word (count)
  4787.      int count;
  4788. {
  4789.   int orig_point = rl_point;
  4790.  
  4791.   if (count < 0)
  4792.     rl_kill_word (-count);
  4793.   else
  4794.     {
  4795.       rl_backward_word (count);
  4796.  
  4797.       if (rl_point != orig_point)
  4798.     rl_kill_text (orig_point, rl_point);
  4799.     }
  4800. }
  4801.  
  4802. /* Kill from here to the end of the line.  If DIRECTION is negative, kill
  4803.    back to the line start instead. */
  4804. rl_kill_line (direction)
  4805.      int direction;
  4806. {
  4807.   int orig_point = rl_point;
  4808.  
  4809.   if (direction < 0)
  4810.     rl_backward_kill_line (1);
  4811.   else
  4812.     {
  4813.       rl_end_of_line ();
  4814.       if (orig_point != rl_point)
  4815.     rl_kill_text (orig_point, rl_point);
  4816.       rl_point = orig_point;
  4817.     }
  4818. }
  4819.  
  4820. /* Kill backwards to the start of the line.  If DIRECTION is negative, kill
  4821.    forwards to the line end instead. */
  4822. rl_backward_kill_line (direction)
  4823.      int direction;
  4824. {
  4825.   int orig_point = rl_point;
  4826.  
  4827.   if (direction < 0)
  4828.     rl_kill_line (1);
  4829.   else
  4830.     {
  4831.       if (!rl_point)
  4832.     ding ();
  4833.       else
  4834.     {
  4835.       rl_beg_of_line ();
  4836.       rl_kill_text (orig_point, rl_point);
  4837.     }
  4838.     }
  4839. }
  4840.  
  4841. /* Yank back the last killed text.  This ignores arguments. */
  4842. rl_yank ()
  4843. {
  4844.   if (!rl_kill_ring) rl_abort ();
  4845.   rl_insert_text (rl_kill_ring[rl_kill_index]);
  4846. }
  4847.  
  4848. /* If the last command was yank, or yank_pop, and the text just
  4849.    before point is identical to the current kill item, then
  4850.    delete that text from the line, rotate the index down, and
  4851.    yank back some other text. */
  4852. rl_yank_pop ()
  4853. {
  4854.   int l;
  4855.  
  4856.   if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
  4857.       !rl_kill_ring)
  4858.     {
  4859.       rl_abort ();
  4860.     }
  4861.  
  4862.   l = strlen (rl_kill_ring[rl_kill_index]);
  4863.   if (((rl_point - l) >= 0) &&
  4864.       (strncmp (the_line + (rl_point - l),
  4865.         rl_kill_ring[rl_kill_index], l) == 0))
  4866.     {
  4867.       rl_delete_text ((rl_point - l), rl_point);
  4868.       rl_point -= l;
  4869.       rl_kill_index--;
  4870.       if (rl_kill_index < 0)
  4871.     rl_kill_index = rl_kill_ring_length - 1;
  4872.       rl_yank ();
  4873.     }
  4874.   else
  4875.     rl_abort ();
  4876.  
  4877. }
  4878.  
  4879. /* Yank the COUNTth argument from the previous history line. */
  4880. rl_yank_nth_arg (count, ignore)
  4881.      int count;
  4882. {
  4883.   register HIST_ENTRY *entry = previous_history ();
  4884.   char *arg;
  4885.  
  4886.   if (entry)
  4887.     next_history ();
  4888.   else
  4889.     {
  4890.       ding ();
  4891.       return;
  4892.     }
  4893.  
  4894.   arg = history_arg_extract (count, count, entry->line);
  4895.   if (!arg || !*arg)
  4896.     {
  4897.       ding ();
  4898.       return;
  4899.     }
  4900.  
  4901.   rl_begin_undo_group ();
  4902.  
  4903. #if defined (VI_MODE)
  4904.   /* Vi mode always inserts a space befoe yanking the argument, and it
  4905.      inserts it right *after* rl_point. */
  4906.   if (rl_editing_mode == vi_mode)
  4907.     rl_point++;
  4908. #endif /* VI_MODE */
  4909.  
  4910.   if (rl_point && the_line[rl_point - 1] != ' ')
  4911.     rl_insert_text (" ");
  4912.  
  4913.   rl_insert_text (arg);
  4914.   free (arg);
  4915.  
  4916.   rl_end_undo_group ();
  4917. }
  4918.  
  4919. /* How to toggle back and forth between editing modes. */
  4920. rl_vi_editing_mode ()
  4921. {
  4922. #if defined (VI_MODE)
  4923.   rl_editing_mode = vi_mode;
  4924.   rl_vi_insertion_mode ();
  4925. #endif /* VI_MODE */
  4926. }
  4927.  
  4928. rl_emacs_editing_mode ()
  4929. {
  4930.   rl_editing_mode = emacs_mode;
  4931.   keymap = emacs_standard_keymap;
  4932. }
  4933.  
  4934.  
  4935. /* **************************************************************** */
  4936. /*                                    */
  4937. /*                 Completion                    */
  4938. /*                                    */
  4939. /* **************************************************************** */
  4940.  
  4941. /* Non-zero means that case is not significant in completion. */
  4942. int completion_case_fold = 0;
  4943.  
  4944. /* Return an array of (char *) which is a list of completions for TEXT.
  4945.    If there are no completions, return a NULL pointer.
  4946.    The first entry in the returned array is the substitution for TEXT.
  4947.    The remaining entries are the possible completions.
  4948.    The array is terminated with a NULL pointer.
  4949.  
  4950.    ENTRY_FUNCTION is a function of two args, and returns a (char *).
  4951.      The first argument is TEXT.
  4952.      The second is a state argument; it should be zero on the first call, and
  4953.      non-zero on subsequent calls.  It returns a NULL pointer to the caller
  4954.      when there are no more matches.
  4955.  */
  4956. char **
  4957. completion_matches (text, entry_function)
  4958.      char *text;
  4959.      char *(*entry_function) ();
  4960. {
  4961.   /* Number of slots in match_list. */
  4962.   int match_list_size;
  4963.  
  4964.   /* The list of matches. */
  4965.   char **match_list =
  4966.     (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
  4967.  
  4968.   /* Number of matches actually found. */
  4969.   int matches = 0;
  4970.  
  4971.   /* Temporary string binder. */
  4972.   char *string;
  4973.  
  4974.   match_list[1] = (char *)NULL;
  4975.  
  4976.   while (string = (*entry_function) (text, matches))
  4977.     {
  4978.       if (matches + 1 == match_list_size)
  4979.     match_list = (char **)xrealloc
  4980.       (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
  4981.  
  4982.       match_list[++matches] = string;
  4983.       match_list[matches + 1] = (char *)NULL;
  4984.     }
  4985.  
  4986.   /* If there were any matches, then look through them finding out the
  4987.      lowest common denominator.  That then becomes match_list[0]. */
  4988.   if (matches)
  4989.     {
  4990.       register int i = 1;
  4991.       int low = 100000;        /* Count of max-matched characters. */
  4992.  
  4993.       /* If only one match, just use that. */
  4994.       if (matches == 1)
  4995.     {
  4996.       match_list[0] = match_list[1];
  4997.       match_list[1] = (char *)NULL;
  4998.     }
  4999.       else
  5000.     {
  5001.       /* Otherwise, compare each member of the list with
  5002.          the next, finding out where they stop matching. */
  5003.  
  5004.       while (i < matches)
  5005.         {
  5006.           register int c1, c2, si;
  5007.  
  5008.           if (completion_case_fold)
  5009.         {
  5010.           for (si = 0;
  5011.                (c1 = to_lower(match_list[i][si])) &&
  5012.                (c2 = to_lower(match_list[i + 1][si]));
  5013.                si++)
  5014.             if (c1 != c2) break;
  5015.         }
  5016.           else
  5017.         {
  5018.           for (si = 0;
  5019.                (c1 = match_list[i][si]) &&
  5020.                (c2 = match_list[i + 1][si]);
  5021.                si++)
  5022.             if (c1 != c2) break;
  5023.         }
  5024.  
  5025.           if (low > si) low = si;
  5026.           i++;
  5027.         }
  5028.       match_list[0] = (char *)xmalloc (low + 1);
  5029.       strncpy (match_list[0], match_list[1], low);
  5030.       match_list[0][low] = '\0';
  5031.     }
  5032.     }
  5033.   else                /* There were no matches. */
  5034.     {
  5035.       free (match_list);
  5036.       match_list = (char **)NULL;
  5037.     }
  5038.   return (match_list);
  5039. }
  5040.  
  5041. /* Okay, now we write the entry_function for filename completion.  In the
  5042.    general case.  Note that completion in the shell is a little different
  5043.    because of all the pathnames that must be followed when looking up the
  5044.    completion for a command. */
  5045. char *
  5046. filename_completion_function (text, state)
  5047.      int state;
  5048.      char *text;
  5049. {
  5050.   static DIR *directory;
  5051.   static char *filename = (char *)NULL;
  5052.   static char *dirname = (char *)NULL;
  5053.   static char *users_dirname = (char *)NULL;
  5054.   static int filename_len;
  5055.  
  5056.   struct direct *entry = (struct direct *)NULL;
  5057.  
  5058.   /* If we don't have any state, then do some initialization. */
  5059.   if (!state)
  5060.     {
  5061.       char *temp;
  5062.  
  5063.       if (dirname) free (dirname);
  5064.       if (filename) free (filename);
  5065.       if (users_dirname) free (users_dirname);
  5066.  
  5067.       filename = savestring (text);
  5068.       if (!*text) text = ".";
  5069.       dirname = savestring (text);
  5070.  
  5071.       temp = rindex (dirname, '/');
  5072.  
  5073.       if (temp)
  5074.     {
  5075.       strcpy (filename, ++temp);
  5076.       *temp = '\0';
  5077.     }
  5078.       else
  5079.     strcpy (dirname, ".");
  5080.  
  5081.       /* We aren't done yet.  We also support the "~user" syntax. */
  5082.  
  5083.       /* Save the version of the directory that the user typed. */
  5084.       users_dirname = savestring (dirname);
  5085.       {
  5086.     char *temp_dirname;
  5087.  
  5088.     temp_dirname = tilde_expand (dirname);
  5089.     free (dirname);
  5090.     dirname = temp_dirname;
  5091.  
  5092.     if (rl_symbolic_link_hook)
  5093.       (*rl_symbolic_link_hook) (&dirname);
  5094.       }
  5095.       directory = opendir (dirname);
  5096.       filename_len = strlen (filename);
  5097.  
  5098.       rl_filename_completion_desired = 1;
  5099.     }
  5100.  
  5101.   /* At this point we should entertain the possibility of hacking wildcarded
  5102.      filenames, like /usr/man/man<WILD>/te<TAB>.  If the directory name
  5103.      contains globbing characters, then build an array of directories to
  5104.      glob on, and glob on the first one. */
  5105.  
  5106.   /* Now that we have some state, we can read the directory. */
  5107.  
  5108.   while (directory && (entry = readdir (directory)))
  5109.     {
  5110.       /* Special case for no filename.
  5111.      All entries except "." and ".." match. */
  5112.       if (!filename_len)
  5113.     {
  5114.       if ((strcmp (entry->d_name, ".") != 0) &&
  5115.           (strcmp (entry->d_name, "..") != 0))
  5116.         break;
  5117.     }
  5118.       else
  5119.     {
  5120.       /* Otherwise, if these match upto the length of filename, then
  5121.          it is a match. */
  5122.         if (((int)entry->d_namlen >= filename_len) &&
  5123.         (strncmp (filename, entry->d_name, filename_len) == 0))
  5124.           {
  5125.         break;
  5126.           }
  5127.     }
  5128.     }
  5129.  
  5130.   if (!entry)
  5131.     {
  5132.       if (directory)
  5133.     {
  5134.       closedir (directory);
  5135.       directory = (DIR *)NULL;
  5136.     }
  5137.       return (char *)NULL;
  5138.     }
  5139.   else
  5140.     {
  5141.       char *temp;
  5142.  
  5143.       if (dirname && (strcmp (dirname, ".") != 0))
  5144.     {
  5145.       temp = (char *)xmalloc (1 + strlen (users_dirname)
  5146.                   + entry->d_namlen);
  5147.       strcpy (temp, users_dirname);
  5148.       strcat (temp, entry->d_name);
  5149.     }
  5150.       else
  5151.     {
  5152.       temp = (savestring (entry->d_name));
  5153.     }
  5154.       return (temp);
  5155.     }
  5156. }
  5157.  
  5158.  
  5159. /* **************************************************************** */
  5160. /*                                    */
  5161. /*            Binding keys                    */
  5162. /*                                    */
  5163. /* **************************************************************** */
  5164.  
  5165. /* rl_add_defun (char *name, Function *function, int key)
  5166.    Add NAME to the list of named functions.  Make FUNCTION
  5167.    be the function that gets called.
  5168.    If KEY is not -1, then bind it. */
  5169. rl_add_defun (name, function, key)
  5170.      char *name;
  5171.      Function *function;
  5172.      int key;
  5173. {
  5174.   if (key != -1)
  5175.     rl_bind_key (key, function);
  5176.   rl_add_funmap_entry (name, function);
  5177. }
  5178.  
  5179. /* Bind KEY to FUNCTION.  Returns non-zero if KEY is out of range. */
  5180. int
  5181. rl_bind_key (key, function)
  5182.      int key;
  5183.      Function *function;
  5184. {
  5185.   if (key < 0)
  5186.     return (key);
  5187.  
  5188.   if (key > 127 && key < 256)
  5189.     {
  5190.       if (keymap[ESC].type == ISKMAP)
  5191.     {
  5192.       Keymap escmap = (Keymap)keymap[ESC].function;
  5193.  
  5194.       key -= 128;
  5195.       escmap[key].type = ISFUNC;
  5196.       escmap[key].function = function;
  5197.       return (0);
  5198.     }
  5199.       return (key);
  5200.     }
  5201.  
  5202.   keymap[key].type = ISFUNC;
  5203.   keymap[key].function = function;
  5204.  return (0);
  5205. }
  5206.  
  5207. /* Bind KEY to FUNCTION in MAP.  Returns non-zero in case of invalid
  5208.    KEY. */
  5209. int
  5210. rl_bind_key_in_map (key, function, map)
  5211.      int key;
  5212.      Function *function;
  5213.      Keymap map;
  5214. {
  5215.   int result;
  5216.   Keymap oldmap = keymap;
  5217.  
  5218.   keymap = map;
  5219.   result = rl_bind_key (key, function);
  5220.   keymap = oldmap;
  5221.   return (result);
  5222. }
  5223.  
  5224. /* Make KEY do nothing in the currently selected keymap.
  5225.    Returns non-zero in case of error. */
  5226. int
  5227. rl_unbind_key (key)
  5228.      int key;
  5229. {
  5230.   return (rl_bind_key (key, (Function *)NULL));
  5231. }
  5232.  
  5233. /* Make KEY do nothing in MAP.
  5234.    Returns non-zero in case of error. */
  5235. int
  5236. rl_unbind_key_in_map (key, map)
  5237.      int key;
  5238.      Keymap map;
  5239. {
  5240.   return (rl_bind_key_in_map (key, (Function *)NULL, map));
  5241. }
  5242.  
  5243. /* Bind the key sequence represented by the string KEYSEQ to
  5244.    FUNCTION.  This makes new keymaps as necessary.  The initial
  5245.    place to do bindings is in MAP. */
  5246. rl_set_key (keyseq, function, map)
  5247.      char *keyseq;
  5248.      Function *function;
  5249.      Keymap map;
  5250. {
  5251.   rl_generic_bind (ISFUNC, keyseq, function, map);
  5252. }
  5253.  
  5254. /* Bind the key sequence represented by the string KEYSEQ to
  5255.    the string of characters MACRO.  This makes new keymaps as
  5256.    necessary.  The initial place to do bindings is in MAP. */
  5257. rl_macro_bind (keyseq, macro, map)
  5258.      char *keyseq, *macro;
  5259.      Keymap map;
  5260. {
  5261.   char *macro_keys;
  5262.   int macro_keys_len;
  5263.  
  5264.   macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
  5265.  
  5266.   if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
  5267.     {
  5268.       free (macro_keys);
  5269.       return;
  5270.     }
  5271.   rl_generic_bind (ISMACR, keyseq, macro_keys, map);
  5272. }
  5273.  
  5274. /* Bind the key sequence represented by the string KEYSEQ to
  5275.    the arbitrary pointer DATA.  TYPE says what kind of data is
  5276.    pointed to by DATA, right now this can be a function (ISFUNC),
  5277.    a macro (ISMACR), or a keymap (ISKMAP).  This makes new keymaps
  5278.    as necessary.  The initial place to do bindings is in MAP. */
  5279. rl_generic_bind (type, keyseq, data, map)
  5280.      int type;
  5281.      char *keyseq, *data;
  5282.      Keymap map;
  5283. {
  5284.   char *keys;
  5285.   int keys_len;
  5286.   register int i;
  5287.  
  5288.   /* If no keys to bind to, exit right away. */
  5289.   if (!keyseq || !*keyseq)
  5290.     {
  5291.       if (type == ISMACR)
  5292.     free (data);
  5293.       return;
  5294.     }
  5295.  
  5296.   keys = (char *)alloca (1 + (2 * strlen (keyseq)));
  5297.  
  5298.   /* Translate the ASCII representation of KEYSEQ into an array
  5299.      of characters.  Stuff the characters into ARRAY, and the
  5300.      length of ARRAY into LENGTH. */
  5301.   if (rl_translate_keyseq (keyseq, keys, &keys_len))
  5302.     return;
  5303.  
  5304.   /* Bind keys, making new keymaps as necessary. */
  5305.   for (i = 0; i < keys_len; i++)
  5306.     {
  5307.       if (i + 1 < keys_len)
  5308.     {
  5309.       if (map[keys[i]].type != ISKMAP)
  5310.         {
  5311.           if (map[i].type == ISMACR)
  5312.         free ((char *)map[i].function);
  5313.  
  5314.           map[keys[i]].type = ISKMAP;
  5315.           map[keys[i]].function = (Function *)rl_make_bare_keymap ();
  5316.         }
  5317.       map = (Keymap)map[keys[i]].function;
  5318.     }
  5319.       else
  5320.     {
  5321.       if (map[keys[i]].type == ISMACR)
  5322.         free ((char *)map[keys[i]].function);
  5323.  
  5324.       map[keys[i]].function = (Function *)data;
  5325.       map[keys[i]].type = type;
  5326.     }
  5327.     }
  5328. }
  5329.  
  5330. /* Translate the ASCII representation of SEQ, stuffing the
  5331.    values into ARRAY, an array of characters.  LEN gets the
  5332.    final length of ARRAY.  Return non-zero if there was an
  5333.    error parsing SEQ. */
  5334. rl_translate_keyseq (seq, array, len)
  5335.      char *seq, *array;
  5336.      int *len;
  5337. {
  5338.   register int i, c, l = 0;
  5339.  
  5340.   for (i = 0; c = seq[i]; i++)
  5341.     {
  5342.       if (c == '\\')
  5343.     {
  5344.       c = seq[++i];
  5345.  
  5346.       if (!c)
  5347.         break;
  5348.  
  5349.       if (((c == 'C' || c == 'M') &&  seq[i + 1] == '-') ||
  5350.           (c == 'e'))
  5351.         {
  5352.           /* Handle special case of backwards define. */
  5353.           if (strncmp (&seq[i], "C-\\M-", 5) == 0)
  5354.         {
  5355.           array[l++] = ESC;
  5356.           i += 5;
  5357.           array[l++] = CTRL (to_upper (seq[i]));
  5358.           if (!seq[i])
  5359.             i--;
  5360.           continue;
  5361.         }
  5362.  
  5363.           switch (c)
  5364.         {
  5365.         case 'M':
  5366.           i++;
  5367.           array[l++] = ESC;
  5368.           break;
  5369.  
  5370.         case 'C':
  5371.           i += 2;
  5372.           array[l++] = CTRL (to_upper (seq[i]));
  5373.           break;
  5374.  
  5375.         case 'e':
  5376.           array[l++] = ESC;
  5377.         }
  5378.  
  5379.           continue;
  5380.         }
  5381.     }
  5382.       array[l++] = c;
  5383.     }
  5384.  
  5385.   *len = l;
  5386.   array[l] = '\0';
  5387.   return (0);
  5388. }
  5389.  
  5390. /* Return a pointer to the function that STRING represents.
  5391.    If STRING doesn't have a matching function, then a NULL pointer
  5392.    is returned. */
  5393. Function *
  5394. rl_named_function (string)
  5395.      char *string;
  5396. {
  5397.   register int i;
  5398.  
  5399.   for (i = 0; funmap[i]; i++)
  5400.     if (stricmp (funmap[i]->name, string) == 0)
  5401.       return (funmap[i]->function);
  5402.   return ((Function *)NULL);
  5403. }
  5404.  
  5405. /* The last key bindings file read. */
  5406. static char *last_readline_init_file = "~/.inputrc";
  5407.  
  5408. /* Re-read the current keybindings file. */
  5409. rl_re_read_init_file (count, ignore)
  5410.      int count, ignore;
  5411. {
  5412.   rl_read_init_file ((char *)NULL);
  5413. }
  5414.  
  5415. /* Do key bindings from a file.  If FILENAME is NULL it defaults
  5416.    to `~/.inputrc'.  If the file existed and could be opened and
  5417.    read, 0 is returned, otherwise errno is returned. */
  5418. int
  5419. rl_read_init_file (filename)
  5420.      char *filename;
  5421. {
  5422.   register int i;
  5423.   char *buffer, *openname, *line, *end;
  5424.   struct stat finfo;
  5425.   int file;
  5426.  
  5427.   /* Default the filename. */
  5428.   if (!filename)
  5429.     filename = last_readline_init_file;
  5430.  
  5431.   openname = tilde_expand (filename);
  5432.  
  5433.   if ((stat (openname, &finfo) < 0) ||
  5434.       (file = open (openname, O_RDONLY, 0666)) < 0)
  5435.     {
  5436.       free (openname);
  5437.       return (errno);
  5438.     }
  5439.   else
  5440.     free (openname);
  5441.  
  5442.   last_readline_init_file = filename;
  5443.  
  5444.   /* Read the file into BUFFER. */
  5445.   buffer = (char *)xmalloc (finfo.st_size + 1);
  5446.   i = read (file, buffer, finfo.st_size);
  5447.   close (file);
  5448.  
  5449.   if (i != finfo.st_size)
  5450.     return (errno);
  5451.  
  5452.   /* Loop over the lines in the file.  Lines that start with `#' are
  5453.      comments; all other lines are commands for readline initialization. */
  5454.   line = buffer;
  5455.   end = buffer + finfo.st_size;
  5456.   while (line < end)
  5457.     {
  5458.       /* Find the end of this line. */
  5459.       for (i = 0; line + i != end && line[i] != '\n'; i++);
  5460.  
  5461.       /* Mark end of line. */
  5462.       line[i] = '\0';
  5463.  
  5464.       /* If the line is not a comment, then parse it. */
  5465.       if (*line != '#')
  5466.     rl_parse_and_bind (line);
  5467.  
  5468.       /* Move to the next line. */
  5469.       line += i + 1;
  5470.     }
  5471.   return (0);
  5472. }
  5473.  
  5474. /* **************************************************************** */
  5475. /*                                    */
  5476. /*            Parser Directives                   */
  5477. /*                                    */
  5478. /* **************************************************************** */
  5479.  
  5480. /* Conditionals. */
  5481.  
  5482. /* Calling programs set this to have their argv[0]. */
  5483. char *rl_readline_name = "other";
  5484.  
  5485. /* Stack of previous values of parsing_conditionalized_out. */
  5486. static unsigned char *if_stack = (unsigned char *)NULL;
  5487. static int if_stack_depth = 0;
  5488. static int if_stack_size = 0;
  5489.  
  5490. /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
  5491. parser_if (args)
  5492.      char *args;
  5493. {
  5494.   register int i;
  5495.  
  5496.   /* Push parser state. */
  5497.   if (if_stack_depth + 1 >= if_stack_size)
  5498.     {
  5499.       if (!if_stack)
  5500.     if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
  5501.       else
  5502.     if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
  5503.     }
  5504.   if_stack[if_stack_depth++] = parsing_conditionalized_out;
  5505.  
  5506.   /* If parsing is turned off, then nothing can turn it back on except
  5507.      for finding the matching endif.  In that case, return right now. */
  5508.   if (parsing_conditionalized_out)
  5509.     return;
  5510.  
  5511.   /* Isolate first argument. */
  5512.   for (i = 0; args[i] && !whitespace (args[i]); i++);
  5513.  
  5514.   if (args[i])
  5515.     args[i++] = '\0';
  5516.  
  5517.   /* Handle "if term=foo" and "if mode=emacs" constructs.  If this
  5518.      isn't term=foo, or mode=emacs, then check to see if the first
  5519.      word in ARGS is the same as the value stored in rl_readline_name. */
  5520.   if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
  5521.     {
  5522.       char *tem, *tname;
  5523.  
  5524.       /* Terminals like "aaa-60" are equivalent to "aaa". */
  5525.       tname = savestring (rl_terminal_name);
  5526.       tem = rindex (tname, '-');
  5527.       if (tem)
  5528.     *tem = '\0';
  5529.  
  5530.       if (stricmp (args + 5, tname) == 0)
  5531.     parsing_conditionalized_out = 0;
  5532.       else
  5533.     parsing_conditionalized_out = 1;
  5534.     }
  5535. #if defined (VI_MODE)
  5536.   else if (strnicmp (args, "mode=", 5) == 0)
  5537.     {
  5538.       int mode;
  5539.  
  5540.       if (stricmp (args + 5, "emacs") == 0)
  5541.     mode = emacs_mode;
  5542.       else if (stricmp (args + 5, "vi") == 0)
  5543.     mode = vi_mode;
  5544.       else
  5545.     mode = no_mode;
  5546.  
  5547.       if (mode == rl_editing_mode)
  5548.     parsing_conditionalized_out = 0;
  5549.       else
  5550.     parsing_conditionalized_out = 1;
  5551.     }
  5552. #endif /* VI_MODE */
  5553.   /* Check to see if the first word in ARGS is the same as the
  5554.      value stored in rl_readline_name. */
  5555.   else if (stricmp (args, rl_readline_name) == 0)
  5556.     parsing_conditionalized_out = 0;
  5557.   else
  5558.     parsing_conditionalized_out = 1;
  5559. }
  5560.  
  5561. /* Invert the current parser state if there is anything on the stack. */
  5562. parser_else (args)
  5563.      char *args;
  5564. {
  5565.   register int i;
  5566.  
  5567.   if (!if_stack_depth)
  5568.     {
  5569.       /* Error message? */
  5570.       return;
  5571.     }
  5572.  
  5573.   /* Check the previous (n - 1) levels of the stack to make sure that
  5574.      we haven't previously turned off parsing. */
  5575.   for (i = 0; i < if_stack_depth - 1; i++)
  5576.     if (if_stack[i] == 1)
  5577.       return;
  5578.  
  5579.   /* Invert the state of parsing if at top level. */
  5580.   parsing_conditionalized_out = !parsing_conditionalized_out;
  5581. }
  5582.  
  5583. /* Terminate a conditional, popping the value of
  5584.    parsing_conditionalized_out from the stack. */
  5585. parser_endif (args)
  5586.      char *args;
  5587. {
  5588.   if (if_stack_depth)
  5589.     parsing_conditionalized_out = if_stack[--if_stack_depth];
  5590.   else
  5591.     {
  5592.       /* *** What, no error message? *** */
  5593.     }
  5594. }
  5595.  
  5596. /* Associate textual names with actual functions. */
  5597. static struct {
  5598.   char *name;
  5599.   Function *function;
  5600. } parser_directives [] = {
  5601.   { "if", parser_if },
  5602.   { "endif", parser_endif },
  5603.   { "else", parser_else },
  5604.   { (char *)0x0, (Function *)0x0 }
  5605. };
  5606.  
  5607. /* Handle a parser directive.  STATEMENT is the line of the directive
  5608.    without any leading `$'. */
  5609. static int
  5610. handle_parser_directive (statement)
  5611.      char *statement;
  5612. {
  5613.   register int i;
  5614.   char *directive, *args;
  5615.  
  5616.   /* Isolate the actual directive. */
  5617.  
  5618.   /* Skip whitespace. */
  5619.   for (i = 0; whitespace (statement[i]); i++);
  5620.  
  5621.   directive = &statement[i];
  5622.  
  5623.   for (; statement[i] && !whitespace (statement[i]); i++);
  5624.  
  5625.   if (statement[i])
  5626.     statement[i++] = '\0';
  5627.  
  5628.   for (; statement[i] && whitespace (statement[i]); i++);
  5629.  
  5630.   args = &statement[i];
  5631.  
  5632.   /* Lookup the command, and act on it. */
  5633.   for (i = 0; parser_directives[i].name; i++)
  5634.     if (stricmp (directive, parser_directives[i].name) == 0)
  5635.       {
  5636.     (*parser_directives[i].function) (args);
  5637.     return (0);
  5638.       }
  5639.  
  5640.   /* *** Should an error message be output? */
  5641.   return (1);
  5642. }
  5643.  
  5644. /* Ugly but working hack for binding prefix meta. */
  5645. #define PREFIX_META_HACK
  5646.  
  5647. static int substring_member_of_array ();
  5648.  
  5649. /* Read the binding command from STRING and perform it.
  5650.    A key binding command looks like: Keyname: function-name\0,
  5651.    a variable binding command looks like: set variable value.
  5652.    A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
  5653. rl_parse_and_bind (string)
  5654.      char *string;
  5655. {
  5656.   extern char *possible_control_prefixes[], *possible_meta_prefixes[];
  5657.   char *funname, *kname;
  5658.   register int c;
  5659.   int key, i;
  5660.  
  5661.   while (string && whitespace (*string))
  5662.     string++;
  5663.  
  5664.   if (!string || !*string || *string == '#')
  5665.     return;
  5666.  
  5667.   /* If this is a parser directive, act on it. */
  5668.   if (*string == '$')
  5669.     {
  5670.       handle_parser_directive (&string[1]);
  5671.       return;
  5672.     }
  5673.  
  5674.   /* If we are supposed to be skipping parsing right now, then do it. */
  5675.   if (parsing_conditionalized_out)
  5676.     return;
  5677.  
  5678.   i = 0;
  5679.   /* If this keyname is a complex key expression surrounded by quotes,
  5680.      advance to after the matching close quote. */
  5681.   if (*string == '"')
  5682.     {
  5683.       for (i = 1; c = string[i]; i++)
  5684.     {
  5685.       if (c == '"' && string[i - 1] != '\\')
  5686.         break;
  5687.     }
  5688.     }
  5689.  
  5690.   /* Advance to the colon (:) or whitespace which separates the two objects. */
  5691.   for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
  5692.  
  5693.   /* Mark the end of the command (or keyname). */
  5694.   if (string[i])
  5695.     string[i++] = '\0';
  5696.  
  5697.   /* If this is a command to set a variable, then do that. */
  5698.   if (stricmp (string, "set") == 0)
  5699.     {
  5700.       char *var = string + i;
  5701.       char *value;
  5702.  
  5703.       /* Make VAR point to start of variable name. */
  5704.       while (*var && whitespace (*var)) var++;
  5705.  
  5706.       /* Make value point to start of value string. */
  5707.       value = var;
  5708.       while (*value && !whitespace (*value)) value++;
  5709.       if (*value)
  5710.     *value++ = '\0';
  5711.       while (*value && whitespace (*value)) value++;
  5712.  
  5713.       rl_variable_bind (var, value);
  5714.       return;
  5715.     }
  5716.  
  5717.   /* Skip any whitespace between keyname and funname. */
  5718.   for (; string[i] && whitespace (string[i]); i++);
  5719.   funname = &string[i];
  5720.  
  5721.   /* Now isolate funname.
  5722.      For straight function names just look for whitespace, since
  5723.      that will signify the end of the string.  But this could be a
  5724.      macro definition.  In that case, the string is quoted, so skip
  5725.      to the matching delimiter. */
  5726.   if (*funname == '\'' || *funname == '"')
  5727.     {
  5728.       int delimiter = string[i++];
  5729.  
  5730.       for (; c = string[i]; i++)
  5731.     {
  5732.       if (c == delimiter && string[i - 1] != '\\')
  5733.         break;
  5734.     }
  5735.       if (c)
  5736.     i++;
  5737.     }
  5738.  
  5739.   /* Advance to the end of the string.  */
  5740.   for (; string[i] && !whitespace (string[i]); i++);
  5741.  
  5742.   /* No extra whitespace at the end of the string. */
  5743.   string[i] = '\0';
  5744.  
  5745.   /* If this is a new-style key-binding, then do the binding with
  5746.      rl_set_key ().  Otherwise, let the older code deal with it. */
  5747.   if (*string == '"')
  5748.     {
  5749.       char *seq = (char *)alloca (1 + strlen (string));
  5750.       register int j, k = 0;
  5751.  
  5752.       for (j = 1; string[j]; j++)
  5753.     {
  5754.       if (string[j] == '"' && string[j - 1] != '\\')
  5755.         break;
  5756.  
  5757.       seq[k++] = string[j];
  5758.     }
  5759.       seq[k] = '\0';
  5760.  
  5761.       /* Binding macro? */
  5762.       if (*funname == '\'' || *funname == '"')
  5763.     {
  5764.       j = strlen (funname);
  5765.  
  5766.       if (j && funname[j - 1] == *funname)
  5767.         funname[j - 1] = '\0';
  5768.  
  5769.       rl_macro_bind (seq, &funname[1], keymap);
  5770.     }
  5771.       else
  5772.     rl_set_key (seq, rl_named_function (funname), keymap);
  5773.  
  5774.       return;
  5775.     }
  5776.  
  5777.   /* Get the actual character we want to deal with. */
  5778.   kname = rindex (string, '-');
  5779.   if (!kname)
  5780.     kname = string;
  5781.   else
  5782.     kname++;
  5783.  
  5784.   key = glean_key_from_name (kname);
  5785.  
  5786.   /* Add in control and meta bits. */
  5787.   if (substring_member_of_array (string, possible_control_prefixes))
  5788.     key = CTRL (to_upper (key));
  5789.  
  5790.   if (substring_member_of_array (string, possible_meta_prefixes))
  5791.     key = META (key);
  5792.  
  5793.   /* Temporary.  Handle old-style keyname with macro-binding. */
  5794.   if (*funname == '\'' || *funname == '"')
  5795.     {
  5796.       char seq[2];
  5797.       int fl = strlen (funname);
  5798.  
  5799.       seq[0] = key; seq[1] = '\0';
  5800.       if (fl && funname[fl - 1] == *funname)
  5801.     funname[fl - 1] = '\0';
  5802.  
  5803.       rl_macro_bind (seq, &funname[1], keymap);
  5804.     }
  5805. #if defined (PREFIX_META_HACK)
  5806.   /* Ugly, but working hack to keep prefix-meta around. */
  5807.   else if (stricmp (funname, "prefix-meta") == 0)
  5808.     {
  5809.       char seq[2];
  5810.  
  5811.       seq[0] = key;
  5812.       seq[1] = '\0';
  5813.       rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, keymap);
  5814.     }
  5815. #endif /* PREFIX_META_HACK */
  5816.   else
  5817.     rl_bind_key (key, rl_named_function (funname));
  5818. }
  5819.  
  5820. rl_variable_bind (name, value)
  5821.      char *name, *value;
  5822. {
  5823.   if (stricmp (name, "editing-mode") == 0)
  5824.     {
  5825.       if (strnicmp (value, "vi", 2) == 0)
  5826.     {
  5827. #if defined (VI_MODE)
  5828.       keymap = vi_insertion_keymap;
  5829.       rl_editing_mode = vi_mode;
  5830. #else
  5831. #if defined (NOTDEF)
  5832.       /* What state is the terminal in?  I'll tell you:
  5833.          non-determinate!  That means we cannot do any output. */
  5834.       ding ();
  5835. #endif /* NOTDEF */
  5836. #endif /* VI_MODE */
  5837.     }
  5838.       else if (strnicmp (value, "emacs", 5) == 0)
  5839.     {
  5840.       keymap = emacs_standard_keymap;
  5841.       rl_editing_mode = emacs_mode;
  5842.     }
  5843.     }
  5844.   else if (stricmp (name, "horizontal-scroll-mode") == 0)
  5845.     {
  5846.       if (!*value || stricmp (value, "On") == 0)
  5847.     horizontal_scroll_mode = 1;
  5848.       else
  5849.     horizontal_scroll_mode = 0;
  5850.     }
  5851.   else if (stricmp (name, "mark-modified-lines") == 0)
  5852.     {
  5853.       if (!*value || stricmp (value, "On") == 0)
  5854.     mark_modified_lines = 1;
  5855.       else
  5856.     mark_modified_lines = 0;
  5857.     }
  5858.   else if (stricmp (name, "prefer-visible-bell") == 0)
  5859.     {
  5860.       if (!*value || stricmp (value, "On") == 0)
  5861.         prefer_visible_bell = 1;
  5862.       else
  5863.         prefer_visible_bell = 0;
  5864.     }
  5865.   else if (stricmp (name, "comment-begin") == 0)
  5866.     {
  5867. #if defined (VI_MODE)
  5868.       extern char *rl_vi_comment_begin;
  5869.  
  5870.       if (*value)
  5871.     {
  5872.       if (rl_vi_comment_begin)
  5873.         free (rl_vi_comment_begin);
  5874.  
  5875.       rl_vi_comment_begin = savestring (value);
  5876.     }
  5877. #endif /* VI_MODE */
  5878.     }
  5879. }
  5880.  
  5881. /* Return the character which matches NAME.
  5882.    For example, `Space' returns ' '. */
  5883.  
  5884. typedef struct {
  5885.   char *name;
  5886.   int value;
  5887. } assoc_list;
  5888.  
  5889. assoc_list name_key_alist[] = {
  5890.   { "DEL", 0x7f },
  5891.   { "ESC", '\033' },
  5892.   { "Escape", '\033' },
  5893.   { "LFD", '\n' },
  5894.   { "Newline", '\n' },
  5895.   { "RET", '\r' },
  5896.   { "Return", '\r' },
  5897.   { "Rubout", 0x7f },
  5898.   { "SPC", ' ' },
  5899.   { "Space", ' ' },
  5900.   { "Tab", 0x09 },
  5901.   { (char *)0x0, 0 }
  5902. };
  5903.  
  5904. int
  5905. glean_key_from_name (name)
  5906.      char *name;
  5907. {
  5908.   register int i;
  5909.  
  5910.   for (i = 0; name_key_alist[i].name; i++)
  5911.     if (stricmp (name, name_key_alist[i].name) == 0)
  5912.       return (name_key_alist[i].value);
  5913.  
  5914.   return (*name);
  5915. }
  5916.  
  5917.  
  5918. /* **************************************************************** */
  5919. /*                                    */
  5920. /*          Key Binding and Function Information            */
  5921. /*                                    */
  5922. /* **************************************************************** */
  5923.  
  5924. /* Each of the following functions produces information about the
  5925.    state of keybindings and functions known to Readline.  The info
  5926.    is always printed to rl_outstream, and in such a way that it can
  5927.    be read back in (i.e., passed to rl_parse_and_bind (). */
  5928.  
  5929. /* Print the names of functions known to Readline. */
  5930. void
  5931. rl_list_funmap_names (ignore)
  5932.      int ignore;
  5933. {
  5934.   register int i;
  5935.   char **funmap_names;
  5936.   extern char **rl_funmap_names ();
  5937.  
  5938.   funmap_names = rl_funmap_names ();
  5939.  
  5940.   if (!funmap_names)
  5941.     return;
  5942.  
  5943.   for (i = 0; funmap_names[i]; i++)
  5944.     fprintf (rl_outstream, "%s\n", funmap_names[i]);
  5945.  
  5946.   free (funmap_names);
  5947. }
  5948.  
  5949. /* Return a NULL terminated array of strings which represent the key
  5950.    sequences that are used to invoke FUNCTION in MAP. */
  5951. static char **
  5952. invoking_keyseqs_in_map (function, map)
  5953.      Function *function;
  5954.      Keymap map;
  5955. {
  5956.   register int key;
  5957.   char **result;
  5958.   int result_index, result_size;
  5959.  
  5960.   result = (char **)NULL;
  5961.   result_index = result_size = 0;
  5962.  
  5963.   for (key = 0; key < 128; key++)
  5964.     {
  5965.       switch (map[key].type)
  5966.     {
  5967.     case ISMACR:
  5968.       /* Macros match, if, and only if, the pointers are identical.
  5969.          Thus, they are treated exactly like functions in here. */
  5970.     case ISFUNC:
  5971.       /* If the function in the keymap is the one we are looking for,
  5972.          then add the current KEY to the list of invoking keys. */
  5973.       if (map[key].function == function)
  5974.         {
  5975.           char *keyname = (char *)xmalloc (5);
  5976.  
  5977.           if (CTRL_P (key))
  5978.         sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
  5979.           else if (key == RUBOUT)
  5980.         sprintf (keyname, "\\C-?");
  5981.           else
  5982.         sprintf (keyname, "%c", key);
  5983.           
  5984.           if (result_index + 2 > result_size)
  5985.         {
  5986.           if (!result)
  5987.             result = (char **) xmalloc
  5988.               ((result_size = 10) * sizeof (char *));
  5989.           else
  5990.             result = (char **) xrealloc
  5991.               (result, (result_size += 10) * sizeof (char *));
  5992.         }
  5993.  
  5994.           result[result_index++] = keyname;
  5995.           result[result_index] = (char *)NULL;
  5996.         }
  5997.       break;
  5998.  
  5999.     case ISKMAP:
  6000.       {
  6001.         char **seqs;
  6002.  
  6003.         /* Find the list of keyseqs in this map which have FUNCTION as
  6004.            their target.  Add the key sequences found to RESULT. */
  6005.         seqs =
  6006.           invoking_keyseqs_in_map (function, (Keymap)map[key].function);
  6007.  
  6008.         if (seqs)
  6009.           {
  6010.         register int i;
  6011.  
  6012.         for (i = 0; seqs[i]; i++)
  6013.           {
  6014.             char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
  6015.  
  6016.             if (key == ESC)
  6017.               sprintf (keyname, "\\e");
  6018.             else if (CTRL_P (key))
  6019.               sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
  6020.             else if (key == RUBOUT)
  6021.               sprintf (keyname, "\\C-?");
  6022.             else
  6023.               sprintf (keyname, "%c", key);
  6024.  
  6025.             strcat (keyname, seqs[i]);
  6026.  
  6027.             if (result_index + 2 > result_size)
  6028.               {
  6029.             if (!result)
  6030.               result = (char **)
  6031.                 xmalloc ((result_size = 10) * sizeof (char *));
  6032.             else
  6033.               result = (char **)
  6034.                 xrealloc (result,
  6035.                       (result_size += 10) * sizeof (char *));
  6036.               }
  6037.  
  6038.             result[result_index++] = keyname;
  6039.             result[result_index] = (char *)NULL;
  6040.           }
  6041.           }
  6042.       }
  6043.       break;
  6044.     }
  6045.     }
  6046.   return (result);
  6047. }
  6048.  
  6049. /* Return a NULL terminated array of strings which represent the key
  6050.    sequences that can be used to invoke FUNCTION using the current keymap. */
  6051. char **
  6052. rl_invoking_keyseqs (function)
  6053.      Function *function;
  6054. {
  6055.   return (invoking_keyseqs_in_map (function, keymap));
  6056. }
  6057.  
  6058. /* Print all of the current functions and their bindings to
  6059.    rl_outstream.  If an explicit argument is given, then print
  6060.    the output in such a way that it can be read back in. */
  6061. int
  6062. rl_dump_functions (count)
  6063.      int count;
  6064. {
  6065.   void rl_function_dumper ();
  6066.  
  6067.   rl_function_dumper (rl_explicit_arg);
  6068.   rl_on_new_line ();
  6069.   return (0);
  6070. }
  6071.  
  6072. /* Print all of the functions and their bindings to rl_outstream.  If
  6073.    PRINT_READABLY is non-zero, then print the output in such a way
  6074.    that it can be read back in. */
  6075. void
  6076. rl_function_dumper (print_readably)
  6077.      int print_readably;
  6078. {
  6079.   register int i;
  6080.   char **rl_funmap_names (), **names;
  6081.   char *name;
  6082.  
  6083.   names = rl_funmap_names ();
  6084.  
  6085.   fprintf (rl_outstream, "\n");
  6086.  
  6087.   for (i = 0; name = names[i]; i++)
  6088.     {
  6089.       Function *function;
  6090.       char **invokers;
  6091.  
  6092.       function = rl_named_function (name);
  6093.       invokers = invoking_keyseqs_in_map (function, keymap);
  6094.  
  6095.       if (print_readably)
  6096.     {
  6097.       if (!invokers)
  6098.         fprintf (rl_outstream, "# %s (not bound)\n", name);
  6099.       else
  6100.         {
  6101.           register int j;
  6102.  
  6103.           for (j = 0; invokers[j]; j++)
  6104.         {
  6105.           fprintf (rl_outstream, "\"%s\": %s\n",
  6106.                invokers[j], name);
  6107.           free (invokers[j]);
  6108.         }
  6109.  
  6110.           free (invokers);
  6111.         }
  6112.     }
  6113.       else
  6114.     {
  6115.       if (!invokers)
  6116.         fprintf (rl_outstream, "%s is not bound to any keys\n",
  6117.              name);
  6118.       else
  6119.         {
  6120.           register int j;
  6121.  
  6122.           fprintf (rl_outstream, "%s can be found on ", name);
  6123.  
  6124.           for (j = 0; invokers[j] && j < 5; j++)
  6125.         {
  6126.           fprintf (rl_outstream, "\"%s\"%s", invokers[j],
  6127.                invokers[j + 1] ? ", " : ".\n");
  6128.         }
  6129.  
  6130.           if (j == 5 && invokers[j])
  6131.         fprintf (rl_outstream, "...\n");
  6132.  
  6133.           for (j = 0; invokers[j]; j++)
  6134.         free (invokers[j]);
  6135.  
  6136.           free (invokers);
  6137.         }
  6138.     }
  6139.     }
  6140. }
  6141.  
  6142.  
  6143. /* **************************************************************** */
  6144. /*                                    */
  6145. /*            String Utility Functions            */
  6146. /*                                    */
  6147. /* **************************************************************** */
  6148.  
  6149. static char *strindex ();
  6150.  
  6151. /* Return non-zero if any members of ARRAY are a substring in STRING. */
  6152. static int
  6153. substring_member_of_array (string, array)
  6154.      char *string, **array;
  6155. {
  6156.   while (*array)
  6157.     {
  6158.       if (strindex (string, *array))
  6159.     return (1);
  6160.       array++;
  6161.     }
  6162.   return (0);
  6163. }
  6164.  
  6165. /* Whoops, Unix doesn't have strnicmp. */
  6166.  
  6167. /* Compare at most COUNT characters from string1 to string2.  Case
  6168.    doesn't matter. */
  6169. static int
  6170. strnicmp (string1, string2, count)
  6171.      char *string1, *string2;
  6172. {
  6173.   register char ch1, ch2;
  6174.  
  6175.   while (count)
  6176.     {
  6177.       ch1 = *string1++;
  6178.       ch2 = *string2++;
  6179.       if (to_upper(ch1) == to_upper(ch2))
  6180.     count--;
  6181.       else break;
  6182.     }
  6183.   return (count);
  6184. }
  6185.  
  6186. /* strcmp (), but caseless. */
  6187. static int
  6188. stricmp (string1, string2)
  6189.      char *string1, *string2;
  6190. {
  6191.   register char ch1, ch2;
  6192.  
  6193.   while (*string1 && *string2)
  6194.     {
  6195.       ch1 = *string1++;
  6196.       ch2 = *string2++;
  6197.       if (to_upper(ch1) != to_upper(ch2))
  6198.     return (1);
  6199.     }
  6200.   return (*string1 | *string2);
  6201. }
  6202.  
  6203. /* Determine if s2 occurs in s1.  If so, return a pointer to the
  6204.    match in s1.  The compare is case insensitive. */
  6205. static char *
  6206. strindex (s1, s2)
  6207.      register char *s1, *s2;
  6208. {
  6209.   register int i, l = strlen (s2);
  6210.   register int len = strlen (s1);
  6211.  
  6212.   for (i = 0; (len - i) >= l; i++)
  6213.     if (strnicmp (&s1[i], s2, l) == 0)
  6214.       return (s1 + i);
  6215.   return ((char *)NULL);
  6216. }
  6217.  
  6218.  
  6219. /* **************************************************************** */
  6220. /*                                    */
  6221. /*            USG (System V) Support                */
  6222. /*                                    */
  6223. /* **************************************************************** */
  6224.  
  6225. /* When compiling and running in the `Posix' environment, Ultrix does
  6226.    not restart system calls, so this needs to do it. */
  6227. int
  6228. rl_getc (stream)
  6229.      FILE *stream;
  6230. {
  6231.   int result;
  6232.   unsigned char c;
  6233.  
  6234.   while (1)
  6235.     {
  6236.       result = read (fileno (stream), &c, sizeof (char));
  6237.  
  6238.       if (result == sizeof (char))
  6239.     return (c);
  6240.  
  6241.       /* If zero characters are returned, then the file that we are
  6242.      reading from is empty!  Return EOF in that case. */
  6243.       if (result == 0)
  6244.     return (EOF);
  6245.  
  6246.       /* If the error that we received was SIGINT, then try again,
  6247.      this is simply an interrupted system call to read ().
  6248.      Otherwise, some error ocurred, also signifying EOF. */
  6249.       if (errno != EINTR)
  6250.     return (EOF);
  6251.     }
  6252. }
  6253.  
  6254. #if defined (STATIC_MALLOC)
  6255.  
  6256. /* **************************************************************** */
  6257. /*                                    */
  6258. /*            xmalloc and xrealloc ()                     */
  6259. /*                                    */
  6260. /* **************************************************************** */
  6261.  
  6262. static void memory_error_and_abort ();
  6263.  
  6264. static char *
  6265. xmalloc (bytes)
  6266.      int bytes;
  6267. {
  6268.   char *temp = (char *)malloc (bytes);
  6269.  
  6270.   if (!temp)
  6271.     memory_error_and_abort ();
  6272.   return (temp);
  6273. }
  6274.  
  6275. static char *
  6276. xrealloc (pointer, bytes)
  6277.      char *pointer;
  6278.      int bytes;
  6279. {
  6280.   char *temp;
  6281.  
  6282.   if (!pointer)
  6283.     temp = (char *)malloc (bytes);
  6284.   else
  6285.     temp = (char *)realloc (pointer, bytes);
  6286.  
  6287.   if (!temp)
  6288.     memory_error_and_abort ();
  6289.  
  6290.   return (temp);
  6291. }
  6292.  
  6293. static void
  6294. memory_error_and_abort ()
  6295. {
  6296.   fprintf (stderr, "readline: Out of virtual memory!\n");
  6297.   abort ();
  6298. }
  6299. #endif /* STATIC_MALLOC */
  6300.  
  6301.  
  6302. /* **************************************************************** */
  6303. /*                                    */
  6304. /*            Testing Readline                */
  6305. /*                                    */
  6306. /* **************************************************************** */
  6307.  
  6308. #if defined (TEST)
  6309.  
  6310. main ()
  6311. {
  6312.   HIST_ENTRY **history_list ();
  6313.   char *temp = (char *)NULL;
  6314.   char *prompt = "readline% ";
  6315.   int done = 0;
  6316.  
  6317.   while (!done)
  6318.     {
  6319.       temp = readline (prompt);
  6320.  
  6321.       /* Test for EOF. */
  6322.       if (!temp)
  6323.     exit (1);
  6324.  
  6325.       /* If there is anything on the line, print it and remember it. */
  6326.       if (*temp)
  6327.     {
  6328.       fprintf (stderr, "%s\r\n", temp);
  6329.       add_history (temp);
  6330.     }
  6331.  
  6332.       /* Check for `command' that we handle. */
  6333.       if (strcmp (temp, "quit") == 0)
  6334.     done = 1;
  6335.  
  6336.       if (strcmp (temp, "list") == 0)
  6337.     {
  6338.       HIST_ENTRY **list = history_list ();
  6339.       register int i;
  6340.       if (list)
  6341.         {
  6342.           for (i = 0; list[i]; i++)
  6343.         {
  6344.           fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
  6345.           free (list[i]->line);
  6346.         }
  6347.           free (list);
  6348.         }
  6349.     }
  6350.       free (temp);
  6351.     }
  6352. }
  6353.  
  6354. #endif /* TEST */
  6355.  
  6356.  
  6357. /*
  6358.  * Local variables:
  6359.  * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"
  6360.  * end:
  6361.  */
  6362.